Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: pls help me with word occurances program

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default pls help me with word occurances program

    i have a program which reads a file and displays the number of occurances of each word in the file and also sorts it occording to number of occurances

    i want to get only top 10 words from the file pls help me with the code

    the program is:
    import java.io.*; 
    import java.util.*; 
     
    public class HashMapEx {
     
        public static void main(String[] args) {         
            LinkedHashMap<String, Integer> wordcount =
                    new LinkedHashMap<String, Integer>();
            try { 
                BufferedReader in = new BufferedReader(
                                          new FileReader("c:\\test\\sample.txt"));
                String str;
     
                while ((str = in.readLine()) != null) { 
                    str = str.toLowerCase(); // convert to lower case 
                    int idx1 = -1;
     
                    for (int i = 0; i < str.length(); i++) { 
     
                        if  ((!Character.isLetter(str.charAt(i))) ||
                             (i + 1 == str.length())) { 
     
                            if (i - idx1 > 1) { 
                                if (Character.isLetter(str.charAt(i))) 
                                    i++;
                                String word = str.substring(idx1 + 1, i);
                                if  (wordcount.containsKey(word)) { 
                                    wordcount.put(word, wordcount.get(word) + 1);
                                } else { 
                                    wordcount.put(word, 1);
                                } 
                            }          
                            idx1 = i;
                        } 
                    } 
                } 
                in.close();
            } catch (Exception e) { 
                e.printStackTrace();
                System.exit(1);
            } 
     
            ArrayList<Integer> values = new ArrayList<Integer>();
            values.addAll(wordcount.values());
     
            Collections.sort(values, Collections.reverseOrder());
     
            int last_i = -1;
     
            for (Integer i : values) { 
                if (last_i == i) // without duplicates
                    continue;
                last_i = i;
     
                for (String s : wordcount.keySet()) { 
                    if (wordcount.get(s) == i) // which have this value  
                        System.out.println(s + ":" + i);
                } 
            } 
        } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: pls help me with word occurances program

    i want to get only top 10 words from the file
    Do you have an algorithm for finding the top words used?
    How are you counting the words and saving those counts?
    Once you have saved the counts then if you sorted them highest count first, you could then pick off the number that you wanted from the top of the list

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: pls help me with word occurances program

    Duplicate post at http://www.javaprogrammingforums.com...html#post39763

Similar Threads

  1. MS word file in Oracle
    By tandonpiyush in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 07:41 AM
  2. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 AM
  3. Word filter assignment help
    By normandygahn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 07:42 AM
  4. How to invert a word String
    By Truffy in forum Java Programming Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM
  5. How to invert a word String
    By Truffy in forum Java Code Snippets and Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM

Tags for this Thread