我无法弄清楚如何为程序找到最常见的单词和最常见的不区分大小写的单词。我有一个扫描仪可以读取文本文件和一个 while 循环,但仍然不知道如何实现我要查找的内容。我是否使用不同的字符串函数来读取和打印单词?
这是我现在的代码:
public class letters {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
String word[] = new String[500];
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
}
}
String []roll = s.split("\\s");
for(int i=0;i<roll.length;i++){
String lin = roll[i];
//System.out.println(lin);
}
}
这就是我到目前为止所拥有的。我需要输出说:
Word:
6 roll
Case-insensitive word:
18 roll
这是我的输入文件:
@
roll tide roll!
Roll Tide Roll!
ROLL TIDE ROLL!
ROll tIDE ROll!
roll tide roll!
Roll Tide Roll!
ROLL TIDE ROLL!
roll tide roll!
Roll Tide Roll !
@
65-43+21= 43
65.0-43.0+21.0= 43.0
65 -43 +21 = 43
65.0 -43.0 +21.0 = 43.0
65 - 43 + 21 = 43
65.00 - 43.0 + 21.000 = +0043.0000
65 - 43 + 21 = 43
我只需要它找到出现次数最多的单词(这是最大的连续字母序列)(即滚动)并打印出它被定位的次数(即 6)。如果有人可以在这方面帮助我,那就太好了!谢谢