我將這些代碼拼湊在一起,當然還有一些幫助。其重點在於接收用戶信息並檢查文本文件,以查看信息中是否有任何單詞也出現在文本文件中(大約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
您需要使用legalEnglishWords.equals(userInput),而不是包含。這將解決您的問題。 –
有一個'Map',其中'String'是'legalEnglishWord'和'Integer'是計數 –
@Learner'legalEnglishWords'是一組字符串。它永遠不會等於單個字符串。 –