0
 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 找不到该文件。这只是类的一部分,但这是发生错误的地方。任何帮助将不胜感激!

4

1 回答 1

0

我相信这是因为String words = scan2.next(); 如果文件没有第 4 行,则会引发此异常。这是我能想到的唯一可能的方法。检查文件是否有第 4 行。

于 2013-09-15T03:03:59.037 回答