Please respect the effort and do submit the code as homework without understanding it completely. Post your doubts.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class WordCount {
public static void main(String[] args) throws Exception {
String filePaths[] = { "first.txt" , "second.txt" , "ifyouhavethird.txt" };
Dictionary<String, Integer> wordCount = new Hashtable<String, Integer>();
int numberOfWords = 0;
for (int i = 0; i < filePaths.length; i++) {
BufferedReader reader = new BufferedReader(new FileReader(
filePaths[i]));
String nextSentence;
while ((nextSentence = reader.readLine()) != null) {
StringTokenizer stringTokenizer = new StringTokenizer(
nextSentence);
while (stringTokenizer.hasMoreTokens()) {
String nextWord = stringTokenizer.nextToken();
numberOfWords++;
Integer count = wordCount.get(nextWord);
if (count != null) {
wordCount.put(nextWord, count + 1);
} else {
wordCount.put(nextWord, 1);
}
}
}
reader.close();
}
System.out.printf("There are %d words \n", numberOfWords);
System.out.printf("There are %d number of unique words \n", wordCount
.size());
System.out.println("Among all the words :");
Enumeration<String> keys = wordCount.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.printf("The word %s Repeated %d Times \n", key,
wordCount.get(key));
}
}
}
~ Sometime java is easier than english
