【发布时间】:2019-04-20 16:36:37
【问题描述】:
我想列出文本文件中的每个唯一单词以及在其中找到每个单词的次数。
我尝试使用if 循环,但我不确定如何在计算后消除已列出的单词。
for (int i = 0; i < words.size(); i++) {
count = 1;
//Count each word in the file and store it in variable count
for (int j = i + 1; j < words.size(); j++) {
if (words.get(i).equals(words.get(j))) {
count++;
}
}
System.out.println("The word " + words.get(i) + " can be
found " + count + " times in the file.");
}
文本文件的内容是“Hello world.Hello world.”,程序将打印以下内容:
The word Hello can be found 2 times in the file.
The word world can be found 2 times in the file.
The word Hello can be found 1 times in the file.
The word world can be found 1 times in the file.
【问题讨论】:
-
words的类型是什么?