Josh Inttap Josh Inttap - 4 years ago 145
Java Question

String array won't add string in a for loop

I am trying to make a hangman game in java. I have tried to make a system that logs the guess in a for loop. I have tried to fix but it won't work.
In eclipse it says that the variable hasn't been initialised, even though it has.
The array not working is lStorage

case 0:
String[] easywords= new String[]{"integer","project","octopus"};
int wrong = 12;
String[] lStorage;
String easyrnd = (easywords[new Random().nextInt(easywords.length)]);
System.out.println("The word has " + easyrnd.length() + " Letters");
System.out.println("\n guess a letter");
String letterguess = consolereader.nextLine();

System.out.println("correct " + letterguess + " is the " + "...number" + "Letter"); //need to put in number of letter
for(int i=12;i>0;i--){

Answer Source

The array actually hasn't been initialized. What you've done is declared it. Try something like this:

String[] lStorage = new String[size];

If this array has to be dynamically sized, I would suggest using a java.util.List or another collection class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download