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));
}
}
}