2012-12-13 56 views
1

我正在創建一個簡單的程序,用於統計紙張中單詞,行和總字符(不包括空格)的數量。這是一個非常簡單的程序。我的文件編譯但是當我運行它,我得到這個錯誤: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; 
} 
} 
+3

Words.txt文件有什麼用? – Thihara

+0

前幾天剛問:http://stackoverflow.com/q/13729294/422353 – madth3

+0

嘗試使用inputStreamReader [java doc](http://docs.oracle.com/javase/6/docs/api/java/ io/FileInputStream.html)而不是掃描儀 – codeMan

回答

0

我不能確切地說沒有看文件的問題的確切原因(也許甚至不是那麼)。

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

是你的問題。如果在條件語句中使用input.hasNextLine(),則使用input.nextLine()。由於您使用的是input.next(),因此您應該在while循環條件語句中使用input.hasNext()

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將提供良好的信息。就此而言。

希望它有幫助。 :)