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 7 of 7

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
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: pls help me with word occurances program

    I just answered this question on the other forum where you posted it.

    http://www.javaprogrammingforums.com...s-program.html

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

    Default Re: pls help me with word occurances program

    @Norm
    but u did not tell me how to remove top 10 elements. if u can give the code snippet to remove top 10 words ur help would be highly appreciated

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: pls help me with word occurances program

    Given a sorted list, look at the list class's API doc for methods you could use to remove items from the list.

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

    Default Re: pls help me with word occurances program

    there we have methods to get first,last, moiddle elements and so on none of it allows us to specify a range of elements like element 1 to element 10

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: pls help me with word occurances program

    we have methods to get ...
    What class are you using?
    If you can get the first, do it 10 times.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: pls help me with word occurances program

    Please do not post duplicate threads. I have locked your other thread and moved this one to the appropriate location.

Similar Threads

  1. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM
  2. MS word file in Oracle
    By tandonpiyush in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 07:41 AM
  3. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 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