Question: Working in Java. So, I have a method that returns an ArrayList of strings based on parsing through a text file. Then, I have another

Working in Java. So, I have a method that returns an ArrayList of strings based on parsing through a text file. Then, I have another method that goes through that arrayList and prints out the elements plus the amount of times they are in the ArrayList (like if "abc" was in the arrayList 3 times, it would print 3). Now, my goal is to sort the ArrayList in descending order based on the number of occurences (I know my current code says duplicates, I am going to change that) and if there is a tie, the tiebreaker is based on alphabetical order. I have the sort code for alphabetical order, but I don't know how to sort based on the number of duplicates in my application. Note, I was thinking that because numbers come before letters (in alphabetical type sort), I could just simply create an arrayList filled with the number of occurences and the element corresponding to it concatenated, but I don't want my final output to print out the number concatenated with the string.

Here is goal output example: say I have an ArrayList that has: "aaa", "bbb", "aaa", "1", "abc", "bbb", "bbb", "1" The output should be: "bbb", "bbb", "bbb", "1", "1", "aaa", "aaa". I think I'm close because I already have the sort for alphabetical... Here is the code:

public ArrayList readFile(File fileName) { ArrayList arrList = new ArrayList(); Scanner sc = null; try { sc = new Scanner(fileName); // Check if there is another line of input while (sc.hasNextLine()) { String str = sc.nextLine(); for (String x : str.split(" ")) { arrList.add(x); } } } catch (IOException exp) { exp.printStackTrace(); } sc.close(); return arrList; } public void countDuplicates(ArrayList strArrList) { for (int i = 0; i < strArrList.size() - 1; i++) { boolean alreadyFound = false; int count = 1; for (int j = 1; j < strArrList.size(); j++) { if ((strArrList.get(i).equals(strArrList.get(j))) && (i != j)) { count++; alreadyFound = true; } } if (alreadyFound) { System.out.println("Dup: " + strArrList.get(i) + ". Amount duplicated: " + count); } } //got dups and amount they are duplicated. Now, I need to sort by number of dups. } public static > void sortArrList(ArrayList gAL) { for(int i = 0; i < gAL.size()-1; i++) { AnyType currentMinVal = gAL.get(i); int minIndex = i; for(int j = i+1; j < gAL.size(); j++) { if(currentMinVal.compareTo(gAL.get(j)) > 0) { currentMinVal = gAL.get(j); minIndex = j; } } AnyType temp = gAL.get(minIndex); gAL.set(minIndex, gAL.get(i)); gAL.set(i, temp); } System.out.println(gAL); } 

I'm trying to write code without using imported methods if possible. So, I'd prefer to not use frequency. Also, I will make sure to thumbs up if I can get at least a small explanation of the code so I can actually understand it. Thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!