Nicht sicher, ob ich das hier veröffentlichen sollte, aber ich bin nicht sicher, ob mein Update in meinem letzten Post gesehen werden kann. Ich habe ein Programm, das ich die Wörter ausdrucken muss, die der Benutzer eingibt, so lange sie in der example.text-Datei (legalEnglishWords) enthalten sind, und sie zählen. Im Moment drucke ich alles im example.text, anstatt nur die vom Benutzer eingegebenen.Zählen Sie Wörter in in 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");
}
}
}
Dies ist die Ausgabe
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
Es tut, was beabsichtigt ist, wurde die Anzahl der Male zu zählen das Wort verwendet, aber er druckt jedes Wort in der Datei. Es fügt auch Wörter vom Benutzer hinzu, die nicht in der Datei enthalten sind, und zählt sie auch (letzte Zeile oben). Das ist nicht was ich will. Jede Hilfe wäre großartig.
Bitte fügen Sie Ihre Eingabe, gewünschte Ausgabe und die Ausgabe, die Sie jetzt erhalten. –
Willkommen bei Stack Overflow! Es sieht so aus, als müssten Sie lernen, einen Debugger zu verwenden. Bitte helfen Sie sich selbst [https://ericlippert.com/2014/03/05/how-to-debug-small-programs/]. Wenn Sie danach immer noch Probleme haben, können Sie gerne mit einem [minimalen, vollständigen und überprüfbaren Beispiel] (http://stackoverflow.com/help/mcve) zurückkommen, das Ihr Problem demonstriert. –
Sie sollten Ihre [erste Frage] (http://stackoverflow.com/q/43509070/4391450) bearbeiten, anstatt sie mit einer Frage zu beantworten und eine neue Frage zu stellen. – AxelH