SRL Archive - Java Help and Tutorials

How to set the lengths of strings


Function

2010-01-09 15:59:03

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

2010-01-09 16:01:11

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

2010-01-09 16:07:39

From: Nava2
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

2010-01-09 16:11:02

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

2010-01-09 17:04:18

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?

2010-01-09 17:04:38

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

2010-01-09 18:27:37

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

2010-01-09 18:49:55

String alphabet[0..25];

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

Function

2010-01-09 22:20:09

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

2010-01-09 22:43:30

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

2010-01-09 22:53:04

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

2010-01-10 00:33:39

From: noidea

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?

2010-01-10 05:43:50

From: senrath
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

2010-01-11 12:50:33

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

2010-01-13 11:16:01

From: Baked0420
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/