问题描述
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 as ArrayList<String>
) and a Scanner
(construct it using new Scanner(new File("wordfile.txt"))
, and use scanner.hasNextLine()
/scanner.nextLine()
) to read the words.
If you indeed need it to be an array, go through list.toArray
in the end.
这篇关于Java的读文件和存储字符串到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!