Results 1 to 2 of 2

Thread: list processing

  1. #1
    kazitula is offline Member
    Join Date
    Oct 2007
    Posts
    10
    Rep Power
    0

    Lightbulb list processing

    I have 2files, each has different contents (like several sentences), how can I count them that how many words are in that files?
    I xplaning it. in one file i've a sentence say "I am learning Java Programming. Java is OOP", in these line I've 8 words, but Java appears here 2 times, so how can I count all of them as Java apperas here twice?
    can anyone please help me??????

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Please respect the effort and do submit the code as homework without understanding it completely. Post your doubts.
    Java 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 &#37;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 :)
    Last edited by roots; 01-22-2008 at 10:38 PM.
    dont worry newbie, we got you covered.

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2008, 08:12 PM
  2. Java Parallel Processing Framework 1.0 RC2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 09:25 PM
  3. Image Processing in J2ME
    By mobeenkhan in forum CLDC and MIDP
    Replies: 1
    Last Post: 08-02-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •