Possible Duplicate:
Storing file content into an array
I’m programming a simple hangman program. I am having difficulty with opening a file, and then storing the data into an array. I can’t seem to figure out how to do it. This is for a school project so if the logic seems unnecessary, just blame the man 🙂
I have a text file (words.txt) with ten words in it. They are in the file with a line in between each of them. They need to be imported into an array. Anyone care to help a young aspiring programmer out? Thanks for any help you can give!
I suggest you use a
List<String>(such asArrayList<String>) and aScanner(construct it usingnew Scanner(new File("wordfile.txt")), and usescanner.hasNextLine()/scanner.nextLine()) to read the words.If you indeed need it to be an array, go through
list.toArrayin the end.