不确定我是否应该在此发帖,但我不确定我的更新是否可以在我的上一篇文章中看到。我有一个程序,我需要打印出用户输入的单词,只要它们包含在example.text文件(legalEnglishWords)中并计算它们。现在我在example.text中打印所有内容,而不仅仅是用户输入的内容。
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
它可以完成预期的操作,计算单词的使用次数,但它会打印文件中的每个单词。它还会添加未包含在文件中的用户的单词并对其进行计数(上一行)。这不是我想要的。任何帮助都会很棒。
答案 0 :(得分: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");
}
答案 1 :(得分:0)
有两点:
打印所有内容:您需要使用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");
同时打印“新”用户输入:您可以通过legalEnglishWords.put(word, 1);
将这些值放入地图中。现在这个单词列在地图中,如果你再次写同一个假单词,它的计数就会增加。如果你想收集假字,你可以创建一个新地图。