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.
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.