Reputation: 146
I am tryiing to make a hangman game in Java, but I am having problems initializing a variable lStorage
. I have added String[] lStorage
,but still not initializing.
switch(difficulty){
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");
while(wrong>=0){
System.out.println("\n guess a letter");
String letterguess = consolereader.nextLine();
if(easyrnd.contains(letterguess)){
System.out.println("correct " + letterguess + " is the " + "...number" + "Letter"); //need to put in number of letter
for(int i=12;i>0;i--){
lStorage[i]=letterguess;
}
Upvotes: 1
Views: 153
Reputation: 869
In Java, arrays are objects (like in C# and probably many other object oriented languages). So, you must create/initialize them using the new
keyword.
Upvotes: 0
Reputation: 2552
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.
Upvotes: 3
Reputation: 1982
You need to initialize your String
array
Try to change this line
String[] lStorage;
to this one
String[] lStorage = new String[12];
Upvotes: 0