Results 1 to 8 of 8

Thread: Sort Frequency

  1. #1
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Unhappy Sort Frequency

    I need to read in a file, store the words in an arraylist and sort it in order to the number of times it appears in arraylist, starting with the most frequent. What is the easiest way to do this only using an arraylist or an iterator?

    I needed to create two classes:
    Word class contains a getWord, getCount, addOneToCount, and toString

    WordList class uses an ArrayList of Word class (not inherit), contains an addWord method, getSize, search, sort, print, and toString.

    I need help writing the sort and the print. In my main, i get a foreach error for the print.

    Someone please help me!!! :(

    Thank You

  2. #2
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Arrays.sort(yourArray);

    I use this to sort an int array. Works beautifully.

    For printing the array...

    for(int i=0;i<yourArray.length();i++){
    //Print to Output, File, etc.
    System.out.println(YourArray[i]);
    }

    Internet Search is a vital part of learning/ figuring out how to make things work...
    Search = Java Sort Array
    Search = Java print array
    Etc...

  3. #3
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Opps, should've read the post more carefully. Your looking for sorting according to parameters. I would do a for loop, iterate through the array, count the number of times a variable exists and store the counts, then you can print accordingly or store according ly.

  4. #4
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    for (WordList list : myList)
    {
    System.out.println(list);
    }

    I get a foreach not applicable to expression type message with this. I have used this code similarly with other variable names and applications but it will not work with this one.

    Also, can anyone help me with the sort? I am sorting Strings by how frequent they appear in a text document

    Thank You

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    for (WordList list : myList)
    {
        System.out.println(list);
    }
    This code assumes that myList is a array (or List) with WordList elements: ie that it is a list of lists. It's quite likely that this is not the case and that you mean something else.

    If your question is about how you would print the elements of some array or collection, post the brief but compilable code you would use for this. If this code results in a compiler message that you can't understand, post the actual message.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    parse your text into a hashmap with key being the word and value being the count. it's all downhill from there.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    parse your text into a hashmap with key being the word and value being the count.
    I agree that a Map would make it straight forward but, alas, from the original post "What is the easiest way to do this only using an arraylist or an iterator?"

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    didn't read the original post that well. go me.

    after reading it a second time, i noticed that you have classes to implement. considering the description of the 'word' class, it's similar to a map, since it apparently has those word and count values stored away.

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 02:03 PM
  3. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 10:50 PM
  4. [SOLVED] BST Frequency Counter
    By theonly in forum Advanced Java
    Replies: 7
    Last Post: 04-29-2008, 11:33 PM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 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
  •