Java-программа, чтобы найти распределительный массив слова

Я хочу найти частоту слова в каждой строке файла. Я хочу сделать это для каждого слова в файле. Я использую BufferedReader и FileReader в Java.

1 ответ

Я рекомендую две вещи:

1) лучше разбери свой вопрос. Вы пытаетесь найти частоту слов в каждой строке? Или частота слов в файле. Как и в случае, если ваш вклад был:

test test dog cat dog dog cat
test cat dog dog cat test

Вы бы хотели:

test: 2, 1
dog: 3, 2
cat: 2, 2

Или ты хочешь

test: 3
dog: 5
cat: 4

2) вот инструменты, которые вам нужны

Map<String,Integer> wordCounts; // store your word counts in your class

/**
 * countWord- a method to signify counting a word
 * @param word the word just counted
 */   
public void countWord(String word) {
    int counts = 0; // assume it does not exist yet
    if(wordCounts.containsKey(word)) { // but if it does, get that count
        counts = wordCounts.get(word);
    } /* end if(contains(word))*/
    wordCounts.put(word,counts + 1); // tell the map to put one more than there was
} /* end countWord */
Другие вопросы по тегам