1

我正在创建一个简单的程序来计算论文中的字数、行数和总字符数(不包括空格)。这是一个非常简单的程序。我的文件可以编译,但是当我运行它时,我收到了这个错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at WordCount.wordCounter(WordCount.java:30)
    at WordCount.main(WordCount.java:16)

有谁知道为什么会这样?

 import java.util.*;
 import java.io.*;
 public class WordCount {
//throws the exception
public static void main(String[] args) throws FileNotFoundException {
    //calls on each counter method and prints each one
    System.out.println("Number of Words: " + wordCounter());
    System.out.println("Number of Lines: " + lineCounter());
    System.out.println("Number of Characters: " + charCounter());

}

//static method that counts words in the text file  
public static int wordCounter() throws FileNotFoundException { 
//inputs the text file
Scanner input = new Scanner(new File("words.txt")); 
    int countWords = 0;
    //while there are more lines
    while (input.hasNextLine()) {
        //goes to each next word
        String word = input.next();
        //counts each word
        countWords++;
    }
    return countWords;
}

//static method that counts lines in the text file  
public static int lineCounter() throws FileNotFoundException {
//inputs the text file
Scanner input2 = new Scanner(new File("words.txt"));
    int countLines = 0;
    //while there are more lines
    while (input2.hasNextLine()) {
        //casts each line as a string
        String line = input2.nextLine();
        //counts each line
        countLines++;
    }
    return countLines;
   }    

//static method that counts characters in the text file 
public static int charCounter() throws FileNotFoundException {
//inputs the text file
Scanner input3 = new Scanner(new File("words.txt"));
    int countChar = 0;
    int character = 0;
    //while there are more lines
    while(input3.hasNextLine()) {
       //casts each line as a string
        String line = input3.nextLine();
        //goes through each character of the line
        for(int i=0; i < line.length(); i++){
            character = line.charAt(i);
             //if character is not a space (gets rid of whitespace)
            if (character != 32){
                //counts each character
                countChar++;
            }
        }           
    }
   return countChar;
}
} 
4

2 回答 2

0
public static int wordCounter() throws FileNotFoundException 
{ 
    Scanner input = new Scanner(new File("words.txt")); 
    int countWords = 0;

    while (input.hasNextLine()) {

        if(input.hasNext()) {
            String word = input.next();
            countWords++;
        }
    }

    return countWords;
}

我刚刚if在 while 循环中添加了一个条件。只需确保检查是否有要解析的令牌。我只在这个地方改变了。只要确保在需要的地方进行更改。

这个链接将提供很好的信息。对此。

希望对您有所帮助。:)

于 2012-12-13T06:25:04.420 回答
0

如果不查看文件,我真的无法说出问题的确切原因(甚至可能不是那样)。

   while (input.hasNextLine()) {
        //goes to each next word
        String word = input.next();
        //counts each word
        countWords++;
    }

是你的问题。如果您input.hasNextLine()在 while 条件语句中使用input.nextLine(). 由于您正在使用input.next(),您应该input.hasNext()在 while 循环条件语句中使用。

于 2012-12-13T06:26:43.713 回答