PDA

View Full Version : How to set the lengths of strings



Function
01-09-2010, 08:59 PM
After reading some SCAR tutorials its


alphabet: array[0..3] of String;


But



String alphabet[0..25];


In java doesn't work lol does anyone know?

Nava2
01-09-2010, 09:01 PM
String theString = new String(LENGTH);

Remember, in Java, different classes have different constructors. So, some you can pass params for others you don't need to!

Function
01-09-2010, 09:07 PM
String theString = new String(LENGTH);

Remember, in Java, different classes have different constructors. So, some you can pass params for others you don't need to!

Thanks a lot! But how do you declare the length :duh: Because 0..25 doesn't work

Nava2
01-09-2010, 09:11 PM
Replace length with the length.. :)

Also, you don't treat Strings the same in Java.

To access chars in a string do:

theString.charAt(charIndex)

Method
01-09-2010, 10:04 PM
You're trying to make an array of String objects, right? You can declare that like this:

String[] alphabet = new String[size];

where size is the length of the array.

Wizzup?
01-09-2010, 10:04 PM
Do you want to set the string to a specific length? Because Java auto resizes strings for you. (It actually creates a new one with the data you need, but that's beyond the point)

IAW, do you have a specific reason for making a string of a specific length?

Bobarkinator
01-09-2010, 11:27 PM
Based on the first SCAR example it looks like he's trying to make an array of strings. If that's the case then Method is right.

mastaraymond
01-09-2010, 11:49 PM
String alphabet[0..25];

I don't think he would like every letter of the alphabet to be a different string..

Function
01-10-2010, 03:20 AM
Ok well I'm dyslexic or was just not paying enough attention. I ment to make the title "How to declare an array of string" not "How to set the lengths of strings" Thanks for all whom have contributed their ideas. I got it :)

Baked0420
01-10-2010, 03:43 AM
I believe there is also:

alphabet.setLength(size); Size being how long the array should be. I think you can do that or something like that, not positive, but for some reason I think it exists, assuming you name your array alphabet of course.

noidea
01-10-2010, 03:53 AM
String[] arr = {"a", "ab", "abc"};
or..
String[] arr = new String[3];//3 is how many slots are in the array
arr[0] = "a";
arr[1] = "ab";
arr[2] = "abc";

note:

You cannot compare Strings in java with ==. Java Strings are not char[]'s.
You must use for example:

String x = "abc";
if (x.equals("abc")) { System.out.println("its the same!"); }
if (x.equalsIgnoreCase("ABC")) { System.out.println("its the same!"); }

String y = "xx1abc1xx";
if (y.contains("abc")) { System.out.println("Works!"); }

if (y.toUpperCase().equals("XX1ABC1XX")) { System.out.println("This changes all the characters in the String, y, to their uppercase value."); }

:)

senrath
01-10-2010, 05:33 AM
You cannot compare Strings in java with ==. Java Strings are not char[]'s.


Well, technically you can. But it's probably not what you want to do.

Wizzup?
01-10-2010, 10:43 AM
Well, technically you can. But it's probably not what you want to do.

a.intern() == b.intern() :)

E: Wouldn't recommend it though.

mrpickle
01-11-2010, 05:50 PM
In java it takes 2 steps (usually) to get an array fully setup. Example.

// Step 1
//
String [][] names;
String [] phoneNumbers;
// the "[]" infront of the varaiable tells the program wheither this is an array or just a // varaible .Two of "[]" makes 2 dimentional arrays, and so on.


// Step 2
// Initiate the variable. This is done so we wont need to hold the variable all the way // from the start. Saves memory & is more flexible
names = new String [100][100]; // Since I use 2 dimensional, I have to set 2 values in two brackets.
phoneNumbers = new String [100];

HyperSecret
01-13-2010, 04:16 PM
I believe there is also:

alphabet.setLength(size); Size being how long the array should be. I think you can do that or something like that, not positive, but for some reason I think it exists, assuming you name your array alphabet of course.

Honestly if you don't know then don't post. Don't come in and confuse someone with something your aren't positive about. Don't try to throw your knowledge of SCAR around and think that it works with other languages.

Also, look through the API it is always useful.
http://java.sun.com/j2se/1.5.0/docs/api/