【发布时间】:2013-11-12 10:48:25
【问题描述】:
如何使用数组计算文本文件中重复的单词?
我的程序能够打印出文件中的总单词,但是我怎样才能让我的程序打印出不同单词的数量,并打印出重复单词的数量列表,如下所示:
蛋糕:4 一个:320 件数:2 24
(大写字母和小写字母的单词被认为是同一个单词)
void FileReader() {
System.out.println("Oppgave A");
int totalWords = 0;
int uniqueWords = 0;
String [] word = new String[35000];
String [] wordC = new String [3500];
try {
File fr = new File("Alice.txt");
Scanner sc = new Scanner (fr);
while(sc.hasNext()){
String words = sc.next();
String[] space = words.split(" ");
String[] comma = words.split(",");
totalWords++;
}
System.out.println("Antall ord som er lest er: " + totalWords);
} catch (Exception e) {
System.out.println("File not found");
}
【问题讨论】:
标签: java arrays string text count