前提・実現したいこと
HashMapを使って、文章中の単語の出現数をカウントするプログラムを作っています。
本来ならば画像のような実行画結果になるはずなのですが、実行しても元の文章が表示されるだけで単語数がカウントされません。
発生している問題・エラーメッセージ
エラーメッセージは表示されていません。
試したこと
以下にプログラムと実行結果、使用した文章を書きます。
プログラム
HashMap<String, Integer> words;
// Create the HashMap
words = new HashMap <String, Integer>();
// Load files
String filename="sample.txt";
String[] lines =loadStrings (filename);
String allText = join(lines, " ").toLowerCase();
String[] tokens =splitTokens (allText, ",.?!:;[]-"'");
for (String s : tokens) {
// Is the word in the HashMap?
if (words.containsKey (s)) {
// Increase the count
words.put(s, words.get (s)+1);
} else {
// Add word to the HashMap
words.put(s, 1);
}
}
for (String val : words.keySet ()) {
print(val + "");
println(words.get (val));
}
使用した文章(sample.txtの中身)
Jared Kushner may have a harder time doing his job now that he s lost access to top secret information, becoming the most high profile victim in a White House security clearance scandal. According to Politico, Kushner and all other White House aides operating at a high level interim security clearance? Top Secret/SCI ? were informed Friday that their clearance would be downgraded.
補足情報(FW/ツールのバージョンなど)
Processingを使用しています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/06 12:36