Reputation: 13
I'm currently working on a HangmanProject in high school. "newWord" is the variable that contains the word chosen in the dictionary file at random. in the makeList method you can see it pull the word randomly. And there is a System.out.println("this: " + newWord); so I can test to see if the word is successfully pulled from the external file. It then returns the newWord, but when back into the main method, it is empty, and therefore empty throughout the rest of the program. Can someone please assist my issue?
public class HangmanProject {
public static void main(String args[]) {
String newWord = " ";
int letterNumber = 0;
makeList(newWord);
countLetters(newWord, letterNumber);
System.out.println("this: " + newWord);
displayBoard(letterNumber, newWord);
}
public static String makeList(String newWord) {
do{
try(Scanner s = new Scanner(new File("dictionary.txt"))) {
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {
list.add(s.next());
}
Random random = new Random();
int index = random.nextInt(list.size());
newWord = list.get(index);
} catch (IOException e) {
System.out.println("Error Found");
}
} while (!(newWord.length() >= 5));
System.out.println("this: " + newWord);
return newWord;
}
Upvotes: 1
Views: 43
Reputation: 311393
You don't do anything with the value you return. Just assign it somewhere and you should be OK. Moreover, since makeList
doesn't use its value, just declare it as a local variable:
String newWord = makeList();
Upvotes: 1