java arrays
Hi, I am making a simple application using a java bean to store information in the user session. Since the applicatin is simple I am storing the passwords in the user session as well (it's only for a school project) but I want to use an array of strings to store the username and passwords. I can declare the array fine by using:
String uname[] = new String[10];
but, I cannot fill the array. I've tried using:
uname[0] = new String("cnyman");
and
uname[1] = "jendavis";
and
String uname[0] = new String("cnyman");
and
String uname[1] = "jendavis";
and more, the last two examples result in NetBeans giving an error:
']' expected
If anyone knows what the prob is that would be helpful
Thanx
[781 byte] By [
Wesaa] at [2007-10-2 7:25:48]

> uname[0] = new String("cnyman");
"new String(oldString)" is redundant. It works but creates a copy of the String object for no good reason. Strings are immutable (unmodifiable) so making multiple copies make no sense.
> String uname[0] = new String("cnyman");
> String uname[1] = "jendavis";
"String foo;" declares a reference (=pointer) to an object.
"String foo[];" declares a pointer to an array of pointers.
"String foo[0];" is just invalid syntax.
> uname[1] = "jendavis";
We have a winner!
If it doesn't work: why? Do you get a compilation error, runtime error, what? Post the error message and your code. Remember to use [code]...[/code] tags (or the "code" button on the forum posting page.)
Hello!
Try this:
String uname[]={"jendavis", "cnyman"}; //here you don't need to declare the length of the array before
//--you can add more names if you want
or:
String uname[] = new String[10];
uname[1] = "jendavis";
in the last excample you don't need to put the word 'String' before the uname[1],
because the uname[] is already defined as String.
sjasja -> i got an error saying: ']' expected at compilation.
xxx_yyy-> i didnt know you could add more after creating the array.
I did figure out what was wrong, I was trying to create the array at the beginning of the class instead of inside a function. Thanx for the help though.
Wesaa at 2007-7-16 21:03:11 >
