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

ファイルを見ないと、問題の正確な理由を正確に言うことはできません(おそらくそうではないかもしれません)。

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

あなたの問題です。input.hasNextLine()while条件ステートメントでを使用している場合は、を使用しますinput.nextLine()。を使用しているので、whileループの条件文で使用input.next()する必要があります。input.hasNext()

于 2012-12-13T06:26:43.713 に答える
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;
}

ifwhile ループ内に条件を追加しました。解析するトークンがあることを確認してください。私はここだけ変わりました。必要に応じて変更してください。

このリンクは良い情報を提供します。それに関して。

お役に立てば幸いです。:)

于 2012-12-13T06:25:04.420 に答える