java - 无法弄清楚我在处理扫描仪、文件输入、nextLine() 等的任务中的错误

标签 java

我正在尝试读取文本文件的每一行中有多少个单词,这是有效的。但是,当我读到文本末尾并且不再存在任何行时,程序会继续寻找另一行并给我一个错误。我不知道如何让它在不存在下一行时停止寻找下一行。

我尝试了一系列 if 语句和 while 循环,都使用 input.hasNextLine()和没有。我还尝试重新排列 aString = input.nextLine() 的位置放置在整个主体中。

public static void main(String[] args) throws FileNotFoundException {
  String fileID = args[0];
  Scanner input = new Scanner(new File(fileID));      
  String aString;
  do {
     aString = input.nextLine();
     if(aString.length() > 0) {
        System.out.println(quoted(aString)+" contains "+
                           wordCount(aString) + " words!");
     }
  } while(aString.length() > 0);
  System.out.println(quoted(aString)+" contains "+
                     wordCount(aString) + " words!");
  System.out.println("Done!!!");
   }

public static int wordCount(String text) {
  int words = 0;
  char firstCh = text.charAt(0);    
  if (firstCh != ' ') {
     words = words + 1;
  }    
  for (int i = 1; i < text.length(); i++) {
     char secondCh = text.charAt(i);
     if (firstCh == ' ' && secondCh != ' ') {
        words = words + 1;
     } 
     firstCh = secondCh;
  }    
  return words;
}

我的输入是正确的,但我收到错误,因为程序一直在寻找下一行(或者我相信是这样)。

"Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal." contains 30 words!

"Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this." contains 72 words!

"But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth." contains 176 words!

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at wordCountDriver.main(wordCountDriver.java:25)

最佳答案

在阅读该行之前,您应该实际检查下一行是否存在,如下所示。

 while(input.hasNext()){
            String aString = input.nextLine();
            if(aString.length() > 0) {
                System.out.println(aString+" contains "+
                        wordCount(aString) + " words!");
            }

        } 

关于java - 无法弄清楚我在处理扫描仪、文件输入、nextLine() 等的任务中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566624/

相关文章:

java - 将文本文件标签解析为 xml - Java

java - Java中的多个生产者和消费者问题(没有BlockingQueue)

java - JPanel 上的组件在 setLayout(null) 时不显示

java - Spring mvc 创建indexservlet作为根路径

java - 计算两个集合之间的交集最有效的方法是什么(Java)?

java - 为什么这个不会投?

java - float Action 按钮点击时会跳转

Java Date Parsing - 如何用 Tues 解析日期?

java - inetAddress 可以与 inet6/IPv6 一起使用吗?

java - Spring Security 和 Facebook OAuth 2.0 与图形 API 集成