【问题标题】:Reading words and numbers out of a text file从文本文件中读取单词和数字
【发布时间】:2015-06-09 03:26:07
【问题描述】:

我正在编写一个程序来读取文本文件并将唯一的单词和数字添加到 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();
    }
}    

【问题讨论】:

    标签: java java.util.scanner java-io


    【解决方案1】:

    这应该可行,您可以使用 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();
    

    【讨论】:

    • 谢谢!迭代器有所帮助。我对它进行了一些调整,它按我想要的方式工作。谢谢!
    【解决方案2】:

    我会使用 Set 而不是 List

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

    【讨论】:

    • 这给了我一个错误:Indexer.java:10:错误:不兼容的类型:ArrayList 无法转换为 Set Set words = new ArrayList() ;
    • 我的错,应该是new HashSet
    • 当我这样做时,我仍然得到 NoSuchElementException。您的回复是应该解决我的问题还是只是为了改进我的代码?
    【解决方案3】:

    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();
    }
    

    【讨论】:

    • 我试过了,没用。我有一个很长的 java 代码作为我的输入,我的输出是“有 1 个唯一的词这些词是:”,然后是整个输入。
    • 哦,我刚刚解决了 NoSuchElementException。使用 Set 而不是 List 绝对是@Pulse9 的关键并被提及。 List 将存储每个单词实例;而 Set 只会存储唯一的实例。
    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多