error: variable array may not be initialized, plz help

Joined
Oct 23, 2010
Messages
1
Reaction score
0
hello all,

I am new to posting and to Java, please don't crucify me.

I am writing a program for class and need to initialize a string array based on user input of array name and length.
However, the array won't initialize until the name matches a predetermined one, then the user picks the length, array gets initialized.

Code looks similar to the following:
String[] checkname= {"start");
String[] name1;
String name = "";
boolean[] test = {false};

while(test[0] == false)
{
prompt for name of array and check versus checkname[0](this continues till I get the name I want)

prompt for length
if(name.equalsIgnoreCase(checkname[0])
{
name1 = new String[length];
test[0] = true;
}
}

now, as long as I leave it like this, the code will compile and run just fine. However, if I try to place a statement such as println(name1.length); outside the while loop I get the error. I need to be able to do this with 4 different arrays and I don't know the order in which they will be initialized(based on user input) and then I need to be able to pass the arrays to another method. Is there a better structure to use. Please note that I am in a beginning Java class, so things like sort/search methods we still have to code ourselves. Please bare that in mind. Thank you for the help anyone offers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top