2016-10-14 60 views
-1

我正在編寫一個程序,它將掃描一個文本文件並計算其中的單詞數。對於賦值的單詞的定義是:'一個單詞是隻包含字母(a,...,z,A,...,Z)的非空字符串,由空格,標點符號,連字符組成的包圍 ,行開始或行結束。 '。從Java中的文本文件中計數單詞

我在java編程方面很新手,到目前爲止我已經設法編寫了這個實例方法,據推測這應該可行。但事實並非如此。

public int wordCount() { 
    int countWord = 0; 
    String line = ""; 
    try { 
     File file = new File("testtext01.txt"); 
     Scanner input = new Scanner(file); 

     while (input.hasNext()) { 
      line = line + input.next()+" "; 
      input.next(); 
     } 
     input.close(); 
     String[] tokens = line.split("[^a-zA-Z]+"); 
     for (int i=0; i<tokens.length; i++){ 
      countWord++; 
     } 
     return countWord; 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return -1; 
} 
+2

什麼是輸入文件的樣子,和你做什麼輸出到目前爲止? – Ash

+1

此代碼:for(int i = 0; i ControlAltDel

+0

輸入文件看起來像這樣。其記事本: 這是第1行。 這是另一行,第2行。 正在下雨,拿傘。 正在下雨。拿傘!真。 第五行很短。他說:「拿傘!」 另一行,行... 他有一個嗎?沒有。 爲什麼不呢? 輸出是目前: java.util.NoSuchElementException \t在java.util.Scanner.throwFor(未知來源) \t在java.util.Scanner.next(未知來源) \t在TextAnalysis16.wordCount(TextAnalysis16 .java:46) \t at TextAnalysis16.main(TextAnalysis16.java:32) 正文中的字數爲:-1 howto linebreak in comments? –

回答

0

從文件Counting words in text file?

int wordCount = 0; 

    while (input.hasNextLine()){ 

     String nextLine = input.nextLine(); 
     Scanner word = new Scanner(nextline); 

     while(word.hasNext()){ 
      wordCount++;  
      word.next(); 
     } 
     word.close(); 
    } 
    input.close(); 
+0

你能解釋一下你的解決方案,而不是僅僅傾銷一些代碼嗎?這樣其他人可以從中學習。 – Robert

+0

你的代碼認爲'shin-chan'是一個單詞,但OP需要它們作爲兩個單詞。 – progyammer

0

唯一可用的字分隔報價是空格和連字符。您可以使用regexsplit()方法。

int num_words = line.split("[\\s\\-]").length; //stores number of words 
System.out.print("Number of words in file is "+num_words); 

正則表達式(正則表達式):

\\s處分割空格/換行符和\\-在連字符字符串。所以只要有空格,換行符或連字符,句子就會被分割。提取的單詞被複制並返回爲一個數組,其數字爲length是文件中單詞的數量。

0
you can use java regular expression. 
You can read http://docs.oracle.com/javase/tutorial/essential/regex/groups.html to know about group 



    public int wordCount(){ 

     String patternToMatch = "([a-zA-z]+)"; 
     int countWord = 0; 
     try { 
     Pattern pattern = Pattern.compile(patternToMatch); 
     File file = new File("abc.txt"); 
     Scanner sc = new Scanner(file); 
     while(sc.hasNextLine()){ 
      Matcher matcher = pattern.matcher(sc.nextLine()); 
      while(matcher.find()){ 
       countWord++; 
      } 
     } 
     sc.close(); 
}catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return countWord > 0 ? countWord : -1; 
    } 
0
void run(String path) 
throws Exception 
{ 
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))) 
    { 
     int result = 0; 

     while (true) 
     { 
      String line = reader.readLine(); 

      if (line == null) 
      { 
       break; 
      } 

      result += countWords(line); 
     } 

     System.out.println("Words in text: " + result); 
    } 
} 

final Pattern pattern = Pattern.compile("[A-Za-z]+"); 

int countWords(String text) 
{ 
    Matcher matcher = pattern.matcher(text); 

    int result = 0; 

    while (matcher.find()) 
    { 
     ++result; 

     System.out.println("Matcher found [" + matcher.group() + "]"); 
    } 

    System.out.println("Words in line: " + result); 

    return result; 
} 
相關問題