2017-04-20 17 views
2

我將這些代碼拼湊在一起,當然還有一些幫助。其重點在於接收用戶信息並檢查文本文件,以查看信息中是否有任何單詞也出現在文本文件中(大約50個常見的英文單詞)。我的問題是我也想計算消息中每個單詞的出現次數。我試圖添加一個int計數器,但我收到錯誤,因爲我無法用一個整數計算字符串對象。如果我只是添加計數器到:如何計算散列圖中的單詞

for (String userInput : message.split(" ")) 
{ 
    if (legalEnglishWords.contains(userInput)) 
    { 
     System.out.println(userInput + " is an English word "); 
     count++; 
     System.out.println(userInput + " occurs " + counter + " times"); 
    } 
} 

我得到的每一個通過法律單詞測試字迭代,留下我與輸出看上去就像

Please enter in a message: 
transformers are is is this 

transformers occurs 0 times 
are is an English word 
are occurs 1 times 
is is an English word 
is occurs 2 times 
is is an English word 
is occurs 3 times 
this is an English word 
this occurs 4 times 

正如你所看到的計數完全錯誤,並且我在消息中出現了多次的單詞。我只喜歡它顯示一次。我雖然關於在上面if語句中加入& &以檢查單詞是否已經打印,但我不知道要放什麼。我也不知道如何統計if語句中的單詞,而不是每次輸入if語句都要計數。所以基本上我需要一些幫助來計算單個單詞,同時只顯示一次「英語」單詞,以及最後發生的次數。我對java還是有些新鮮的,並且理解我的程序中發生的一切以及爲什麼。所以如果可能的話,我想嘗試一下,只是增加這些代碼來做必要的事情,而不是徹底改變我可能不瞭解的事情。非常感謝你!

import java.io.*; 
import java.util.HashSet; 
import java.util.Scanner; 

public class Affine_English3 
{  
     public static void main(String[] args) throws IOException 
     { 
       HashSet<String> legalEnglishWords = new HashSet<String>(); 
       Scanner file = new Scanner(new File("example.txt")); 
       int counter = 0; 

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

       for (String word : line.split(" ")) 
       { 
        { 
         legalEnglishWords.add(word); 
        } 
       } 
       } 

       file.close(); 

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

       for (String userInput : message.split(" ")) 
       { 
        if (legalEnglishWords.contains(userInput)) 
        { 
         System.out.println(userInput + " is an English word "); 
         counter++; 
        } 
        System.out.println(userInput + " occurs " + counter + " times"); 
       } 
     } 
} 

這是我的「通用英文單詞」文本文件

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

您需要使用legalEnglishWords.equals(userInput),而不是包含。這將解決您的問題。 –

+1

有一個'Map ',其中'String'是'legalEnglishWord'和'Integer'是計數 –

+1

@Learner'legalEnglishWords'是一組字符串。它永遠不會等於單個字符串。 –

回答

3

您是在正確的道路上,但您需要更改集合存儲數據的副本。如果你想爲每個單詞計數,那麼在java中這需要Map。地圖將一組鍵映射到一組值 - 就像將名稱映射到年齡段一樣。在你的情況下,你想把單詞映射到計數。

所以與地圖開始聲明如下:

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

然後每次你遇到,你可以這樣做一個字的時間:如果您使用的是Java 8

if (!wordCounts.containsKey(word)) 
    wordCounts.put(word, 1); 
else 
    wordCounts.put(word, wordCounts.get(word) + 1); 

存在(可以說)更優雅的語法:

wordCounts.compute(word, n -> n == null ? 1 : n + 1); 

處理完所有文本後,您可以打印出第e counts:

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

因此,我已經完成了一些更新以代碼 – Nolan

+0

Downvote,而不是通過錯誤的操作upvote。我編輯了你的答案以糾正它 – davidxxx

+0

@davidxxx你能解釋一下你的downvote多一點嗎?據我所知,你在編輯時只是改變了「你有」,儘管這種收縮自16世紀以來就在使用中(https://www.merriam-webster.com/dictionary/)你)) – sprinter

1

你要做的是跟蹤每個合法的英語單詞(在example.txt中定義)的計數。所以你需要Map而不是Set

在僞代碼,它看起來像:

Map<String, Integer> legalEnglishWord = new HashMap<>(); 
for each word in "exmample.txt" { 
    legalEnglishWords.put(word, 0); 
} 

// handle input 
for each word in inputMessage { 
    if (legalEnglishWords.containsKey(word)) { 
     legalEnglishWords.put(word, legalEnglishWord.get(word) + 1); 
    } 
    // or simply 
    legalEnglishWords.computeIfPresent(word, count -> return count+1); 
} 

// At this point, you have each legal words, and the appearances of 
// each legal word in the input message.