private void readWords() throws IOException {
initialCt = readFrom("C:\\Curses1.txt", initialWords);
midCt = readFrom("C:\\Curses2.txt", middleWords);
endCt = readFrom("C:\\Curses3.txt", endingWords);
} // readWords()
// Pre: fileName is the name of an input file. Tokens (items) in the
// file are strings separated by whitespace.
// Pre: wordList has been allocated and is large enough to hold all
// the tokens in the input file.
// Post: wordList contains all the tokens in the input file, one token
// per string.
private int readFrom(String fileName, String[] wordList) throws IOException {
int count= 0;
FileInputStream fstream = new FileInputStream(fileName);
Scanner scan2 = new Scanner (fstream);
while (scan2.hasNext()){
initialWords[count] =scan2.nextLine();
middleWords[count] = scan2.nextLine();
endingWords [count] = scan2.nextLine();
String words = scan2.next();
++count;
}
return count;
} // readFrom()
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Curses.readFrom(Curses.java:95)
at Curses.readWords(Curses.java:63)
at Curses.run(Curses.java:51)
at Curses.main(Curses.java:131)
我正在尝试读取三个不同的文本文件,并将它们的值存储在三个不同的数组中,但java无法找到该文件。这只是类的一部分,但这是发生错误的地方。任何帮助将不胜感激!
答案 0 :(得分:0)
我相信它正在发生,因为String words = scan2.next();
如果文件没有第4行,则抛出此异常。这是我能想到的唯一可行方式。如果有第4行,请检查文件。