Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-22-2008, 01:39 PM
Member
 
Join Date: Oct 2007
Posts: 10
kazitula is on a distinguished road
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??????
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-22-2008, 10:35 PM
roots's Avatar
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 10:38 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT processing in Java - II JavaForums Java Blogs 0 04-17-2008 11:30 AM
XSLT processing in Java - I JavaForums Java Blogs 0 04-17-2008 11:30 AM
want to make a menu bar for image processing vidhi Java 2D 1 01-15-2008 08:12 PM
Java Parallel Processing Framework 1.0 RC2 JavaBean Java Announcements 0 11-27-2007 09:25 PM
Image Processing in J2ME mobeenkhan CLDC and MIDP 1 08-02-2007 06:30 PM


All times are GMT +3. The time now is 03:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org