Java简单计数文件中的单词

时间:2012-12-13 06:17:05

标签: java

我正在创建一个简单的程序,用于计算纸张中单词,行数和总字符数(不包括空格)。这是一个非常简单的程序。我的文件编译但是当我运行它时我得到这个错误:

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

2 个答案:

答案 0 :(得分: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;
}

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

这个link会提供很好的信息。就此而言。

希望它有用。 :)

答案 1 :(得分:0)

我不能在没有查看文件的情况下确切地说出问题的确切原因(也许不是那么)。

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

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