Results 1 to 2 of 2
Thread: list processing
- 01-22-2008, 12:39 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 10
- Rep Power
- 0
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??????
- 01-22-2008, 09:35 PM #2
Please respect the effort and do submit the code as homework without understanding it completely. Post your doubts.
~ Sometime java is easier than english :)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 %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)); } } }Last edited by roots; 01-22-2008 at 09:38 PM.
dont worry newbie, we got you covered.
Similar Threads
-
want to make a menu bar for image processing
By vidhi in forum Java 2DReplies: 1Last Post: 01-15-2008, 07:12 PM -
Java Parallel Processing Framework 1.0 RC2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:25 PM -
Image Processing in J2ME
By mobeenkhan in forum CLDC and MIDPReplies: 1Last Post: 08-02-2007, 05:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks