0

I am working on a program where I have to count the frequency of food items in a file in order to sort them in descending order.

For example: if my file has ( pizza, ice_cream, pasta, pizza )

I want my program to print something similar to this:

1 ice_cream 1 pasta 2 pizza

I am using a bubble sort algorithm but it seems that I am missing something for this algorithm to work. Any help will be greatly appreciated!

Within class Listabc, I have two local variables and a method called "compareTo."

class Listabc {

     int count = 1;
     String item;


     int compareTo(Listabc listabc) {
        return 0;
     }

}

Within my main method, I have a bubble sort algorithm to sort the food items in a descending order

public class MainMethod {

   public static void main(String[] args) throws FileNotFoundException {

       Scanner input = new Scanner(new BufferedReader(new FileReader("file.txt")));

       List<Listabc> lists = new ArrayList<Listabc>();

       for (int a = 0; a < lists.size() - 1; ++a) {
           for (int b = a + 1; b < lists.size(); b++) {
               if ((lists.get(b)).compareTo(lists.get(a)) > 0) {
                   Listabc temp = lists.get(a);
                   lists.set(a, lists.get(b));
                   lists.set(b, temp);
               }
           }
           System.out.println(lists.get(a));
       }

     }
 }
1
  • Start by indenting your code. Then provide more details than "I am missing something for this algorithm to work". You should analyze the results: give sample inputs, expected outputs and actual outputs. Commented May 8, 2014 at 8:25

3 Answers 3

5

Your implementation of compareTo method is broken. You need to change it by applying a real comparison:

int compareTo(Listabc listabc) {
    return 0; //this means every element is "similar" to another
}

Here's an example about comparing the elements by the item field:

int compareTo(Listabc listabc) {
    return this.item.compareTo(listabc.item);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you make Listabc implement comparable you can just call Collections.sort(lists)

Comments

0

Below program will sort the list using Collections.sort

    public class SortMap {

    public static void main(String[] args) {
    Map<String, Integer> t = new HashMap<String, Integer>();
    Scanner sc = null;
    try {
        sc = new Scanner(new File("foodlist.txt"));
        while (sc.hasNext()) {
            String item = sc.next();
            if (t.get(item) != null) {
                Integer count = t.get(item);
                t.put(item,++count);
            } else {
                t.put(item, 1);
            }

        }
        Set<Map.Entry<String, Integer>>  mp = t.entrySet();
        List<Map.Entry<String, Integer>> ll = new ArrayList<Map.Entry<String,Integer>>(mp);
        Collections.sort(ll, new SortMap.ValueComparator());

        System.out.println(ll);


    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        sc.close();
    }

}

static class ValueComparator implements Comparator<Map.Entry<String, Integer>> {

    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
        return o1.getValue().compareTo(o2.getValue());
    }


}

   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.