import java.util.*; import java.io.*; public class MedalCount3 { public static void main(String[] args) throws IOException { PrintStream output = System.out; Scanner input = new Scanner(System.in); List country = new ArrayList(); List goldMedals = new ArrayList(); List silverMedals = new ArrayList(); List bronzeMedals = new ArrayList(); // read the input using a scanner final String INPUT_NAME = "medals.txt"; Scanner in = new Scanner(new File(INPUT_NAME)); while (in.hasNextLine()) { String line = in.nextLine(); if (line.isEmpty()) { continue; } String regex = ","; String[] s = line.split(regex); // constants to index into s final int EVENT = 0; final int GOLD = 1; final int SILVER = 2; final int BRONZE = 3; String event = s[EVENT]; String goldCountry = s[GOLD]; String silverCountry = s[SILVER]; String bronzeCountry = s[BRONZE]; // update the gold medal winning country int index = country.indexOf(goldCountry); if (index == -1) { country.add(goldCountry); goldMedals.add(1); silverMedals.add(0); bronzeMedals.add(0); } else { int numGold = goldMedals.get(index); goldMedals.set(index, numGold + 1); } // update the silver medal winning country index = country.indexOf(silverCountry); if (index == -1) { country.add(silverCountry); goldMedals.add(0); silverMedals.add(1); bronzeMedals.add(0); } else { int numSilver = silverMedals.get(index); silverMedals.set(index, numSilver + 1); } // update the bronze medal winning country index = country.indexOf(bronzeCountry); if (index == -1) { country.add(bronzeCountry); goldMedals.add(0); silverMedals.add(0); bronzeMedals.add(1); } else { int numBronze = bronzeMedals.get(index); bronzeMedals.set(index, numBronze + 1); } } // gold medal counts TreeSet ascendingGoldCounts = new TreeSet(goldMedals); Set goldCounts = ascendingGoldCounts.descendingSet(); // For each number of gold medals won (from largest to smallest) for (Integer numGold : goldCounts) { Set result = new TreeSet(); // Find the countries that won numGold for (int i = 0; i < country.size(); i++) { Integer thisGold = goldMedals.get(i); if (thisGold.equals(numGold)) { Integer thisSilver = silverMedals.get(i); Integer thisBronze = bronzeMedals.get(i); String thisResult = String.format("%s %2s %2s %2s", country.get(i), thisGold.toString(), thisSilver.toString(), thisBronze.toString()); result.add(thisResult); } } for (String s : result) { output.printf("%34s%n", s); } } } }