如何使用bufferedreader和treemap在文本中打印最常用的单词? -Java

文本将仅包含空格和单词。 输入: 感谢您的帮助\ n 这辆车现在可以工作了\ n 谢谢\ n 输出: --因为它比“谢谢”小。

import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.io.InputStreamReader;
import java.io.*;

public class Main {
   public static void main(String[] args)throws IOException {
String line;
String[] words = new String[100];
Map<String, Integer> frequency = new HashMap<>();
BufferedReader reader = new BufferedReader(new 
 InputStreamReader(System.in));
while ((line = reader.readLine()) != null) {
        line = line.trim();
    if(!line.isEmpty())
    {
        words = line.split("\\W+");
       for(String word : words) {
                String processed = word.toLowerCase();
                processed = processed.replace(",", "");

                if(frequency.containsKey(processed)) {
                    frequency.put(processed, 
                            frequency.get(processed) + 1);
                } else {
                    frequency.put(processed, 1);
                }
       }
    }
}
int mostFrequentlyUsed = 0;
    String theWord = null;

    for(String word : frequency.keySet()) {
        Integer theVal = frequency.get(word);
        if(theVal > mostFrequentlyUsed) {
            mostFrequentlyUsed = theVal;
            theWord = word;
        }
   else if (theVal == mostFrequentlyUsed && word.length() < 
                                                 theWord.length()) {
     theWord = word; mostFrequentlyUsed = theVal;
   }

        }
    System.out.printf(theWord);
      }
   }

我没有通过一项测试,我真的不知道为什么这就是为什么我需要另一种方法。