Thread: list processing
View Single Post
  #2 (permalink)  
Old 01-22-2008, 11:35 PM
roots's Avatar
roots roots is offline
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Please respect the effort and do submit the code as homework without understanding it completely. Post your doubts.
Code:
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
__________________
dont worry newbie, we got you covered.

Last edited by roots : 01-22-2008 at 11:38 PM.
Reply With Quote