从文本文件中读取单词和数字

Reading words and numbers out of a text file

我正在编写一个程序来读取文本文件并将唯一的单词和数字添加到 ArrayList。我为此使用了分隔符,但是当我 运行 程序时,我得到了一个 NoSuchElementException。是我的分隔符错了还是我又犯了一个错误?

这是我的程序:

import java.util.*;
import java.io.*;
public class Indexer
{
   public static void main(String[] args) throws FileNotFoundException
   {

      Scanner fileScanner = new Scanner(new File("File.txt")).useDelimiter("[.,:;()?!\" \t]+~\s");
      int totalWordCount = 0;
      ArrayList<String> words = new ArrayList<String>();
      while ((fileScanner.hasNext()) && (!words.contains(fileScanner.next())))
      {
         words.add(fileScanner.next());
         totalWordCount++;
      }
      System.out.println("There are " + totalWordCount + " unique word(s)");
      System.out.println("These words are:");
      System.out.println(words.toString());
      fileScanner.close();
    }
}    

我会使用 Set 而不是 List

Set<String> words = new HashSet<String>();
while (fileScanner.hasNext()) { 
      words.add(fileScanner.next());

这应该可以,您可以使用 tostring 或迭代器来显示单词:

Set<String> words = new HashSet<String>();
      while ((fileScanner.hasNext())) { 
               words.add(fileScanner.next());
      }
      System.out.println("There are " +  words.size() + " unique word(s)");
      System.out.println("These words are:");
      //System.out.println(words.toString());
      for (Iterator<String> it = words.iterator(); it.hasNext(); ) {
          String f = it.next();
          System.out.println(f);
      }
      fileScanner.close();

NoSuchElementException 可能来自 while 循环内的第二个 fileScanner.next()。

当到达文件的最后一个元素时,它从 while 循环条件中的 fileScanner.next() 读取,导致在循环内进行第二次 fileScanner 调用时没有剩余元素.

一个解决方案可能是每次迭代调用一次 fileScanner.next():

  Scanner fileScanner = new Scanner(new File("File.txt")).useDelimiter("[.,:;()?!\" \t]+~\s");
  int totalWordCount = 0;
  Set<String> words = new HashSet<String>();
  String nextWord;
  while ((fileScanner.hasNext()) && (!words.contains(nextWord = fileScanner.next())))
  {
     words.add(nextWord);
     totalWordCount++;
  }
  System.out.println("There are " + totalWordCount + " unique word(s)");
  System.out.println("These words are:");
  System.out.println(words.toString());
  fileScanner.close();
}