I'm making a basic hangman program in java. I have a WORDS
String that's an array. Simple. I wanted to replace that WORDS array with a .txt file.
public static String readFileAsString(String fileName) {
try {
text = new String(Files.readAllBytes(Paths.get("/Users/veronica/Documents/Alex/Projects/java/projects/user-hangman/src/words.txt")));
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
The file contains plain words, for example:
a
ability
able
When I try to use it in my hangman program, the game goes like this: it selects a random word from the .txt file and prints it in System.out
. But since, the file is just a simple .txt file with plain words, the game thinks that the whole .txt file is one String. How could I make each individual word in some sort of String for itself and make it work on my current program?