2017-04-20 80 views
0

不知道是否應該在此發佈此信息,但我不確定是否可以在我上一篇文章中看到我的更新。我有一個程序,我需要輸出用戶輸入的單詞,只要它們包含在example.text文件(legalEnglishWords)中並對它們進行計數即可。現在我在example.text中打印所有內容,而不僅僅是用戶輸入的內容。在hashmap中計數單詞

import java.io.*; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Affine_English2 

{ 
    public static void main(String[] args) throws IOException 
    { 

     Map<String, Integer> legalEnglishWords = new HashMap<>(); 

     Scanner file = new Scanner(new File("example.txt")); 

      while (file.hasNextLine()) 
      { 
       String line = file.nextLine(); 

      for (String word : line.split(" ")) 
      { 
       { 
        legalEnglishWords.put(word, 0); 
       } 
      } 
      } 

      file.close(); 

      Scanner scan = new Scanner(System.in); 
      System.out.println("Please enter in a message: "); 
      String message = scan.nextLine(); 
      System.out.println(""); 
      scan.close(); 

      for (String userInput : message.split(" ")) 
      { 
       if (legalEnglishWords.containsKey(userInput)) 
       { 
        System.out.println("\"" +userInput + "\" is an English word "); 
       } 

      } 
      System.out.println(""); 

      for(String word : message.split(" ")) 
      { 
       if (!legalEnglishWords.containsKey(word)) 
       { 
        legalEnglishWords.put(word, 1); 
       } 
       else 
       { 
        legalEnglishWords.put(word, legalEnglishWords.get(word) + 1); 
       } 
      } 

      for (String word: legalEnglishWords.keySet()) 
       { 
        System.out.println("the word \"" + word + "\" occurred " + legalEnglishWords.get(word) + " times"); 
       } 
    } 
} 

這是輸出

Please enter in a message: 
aaat is is this are that 

"is" is an English word 
"is" is an English word 
"this" is an English word 
"are" is an English word 
"that" is an English word 

the word "but" occurred 0 times 
the word "" occurred 0 times 
the word "use" occurred 0 times 
the word "had" occurred 0 times 
the word "do" occurred 0 times 
the word "your" occurred 0 times 
the word "when" occurred 0 times 
the word "that" occurred 1 times 
the word "his" occurred 0 times 
the word "from" occurred 0 times 
the word "if" occurred 0 times 
the word "you" occurred 0 times 
the word "they" occurred 0 times 
the word "all" occurred 0 times 
the word "which" occurred 0 times 
the word "in" occurred 0 times 
the word "this" occurred 1 times 
the word "is" occurred 2 times 
the word "it" occurred 0 times 
the word "an" occurred 0 times 
the word "each" occurred 0 times 
the word "she" occurred 0 times 
the word "as" occurred 0 times 
the word "at" occurred 0 times 
the word "word" occurred 0 times 
the word "be" occurred 0 times 
the word "line" occurred 0 times 
the word "for" occurred 0 times 
the word "their" occurred 0 times 
the word "we" occurred 0 times 
the word "can" occurred 0 times 
the word "how" occurred 0 times 
the word "not" occurred 0 times 
the word "are" occurred 1 times 
the word "and" occurred 0 times 
the word "of" occurred 0 times 
the word "by" occurred 0 times 
the word "have" occurred 0 times 
the word "where" occurred 0 times 
the word "said" occurred 0 times 
the word "on" occurred 0 times 
the word "a" occurred 0 times 
the word "or" occurred 0 times 
the word "was" occurred 0 times 
the word "i" occurred 0 times 
the word "the" occurred 0 times 
the word "with" occurred 0 times 
the word "what" occurred 0 times 
the word "there" occurred 0 times 
the word "to" occurred 0 times 
the word "he" occurred 0 times 
the word "aaat" occurred 1 times 

它做什麼打算,計次使用這個詞的數量,但它打印文件中的每一個字。它還添加了未包含在該文件中的用戶的單詞並對它們進行計數(上面的最後一行)。這不是我想要的。任何幫助都會很棒。

+1

請粘貼你的輸入,慾望輸出和你現在得到的輸出。 –

+0

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然遇到問題,請隨時返回一個[最小,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –

+0

您應該編輯您的[第一個問題](http://stackoverflow.com/q/43509070/4391450),而不是回答問題併發布新問題。 – AxelH

回答

0

我想你不得不刪除一行:

if (!legalEnglishWords.containsKey(word)){ 
legalEnglishWords.put(word, 1);// this adds unknown words to your dictionary 
} 

應該再像這樣(短版):

 for (String userInput : message.split(" ")) { 
      if (legalEnglishWords.containsKey(userInput)) { 
       System.out.println("\"" + userInput + "\" is an English word "); 
       legalEnglishWords.put(userInput, legalEnglishWords.get(userInput) + 1); 
      } 
     } 

     System.out.println(""); 

     for (String word : legalEnglishWords.keySet()) { 
      System.out.println("the word \"" + word + "\" occurred " + legalEnglishWords.get(word) + " times"); 
     } 
+0

非常感謝!我沒有注意到加號是將這個單詞添加到我的單詞集中 – Nolan

0

有兩點:

  1. 打印所有內容:您需要使用int值> 0(提示:使用entrySet()來過濾值抗皺性能)

    for (Entry<String, Integer> word : legalEnglishWords.entrySet()) 
        if (word.getValue() > 0) 
         System.out.println("the word \"" + word.getKey() + "\" occurred " + word.getValue() + " times"); 
    
  2. 而且打印的「新」用戶輸入:你把這些值代入通過legalEnglishWords.put(word, 1);地圖。現在這個詞在地圖上列出,如果你再次寫了同樣的假詞,它的計數將會增加。如果你想收集錯誤的單詞,你可以創建一個新的地圖。