Я хочу перечислить каждое уникальное слово в текстовом файле и сколько раз каждое слово встречается в нем.Я попытался использовать цикл 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.