import java.util.TreeMap; import java.util.Scanner; import java.io.PrintStream; public class Points { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); // Use a TreeMap so that the names are sorted alphabetically TreeMap goalsScored = new TreeMap(); while (input.hasNext()) { try { String name = input.next(); int goals = Integer.parseInt(input.next()); Integer oldGoals = goalsScored.get(name); if (oldGoals != null) { goals = oldGoals + goals; } goalsScored.put(name, goals); } catch (NumberFormatException ex) { // ignore bad numbers; do nothing } } // OUTPUT METHOD 1 // Output in reverse alphabetic order: using a StringBuffer you // just iterate over the key set and insert into the front of the // StringBuffer output.println(); output.println("Output using StringBuffer"); StringBuffer result = new StringBuffer(); for (String name : goalsScored.keySet()) { String thisResult = name + ":" + goalsScored.get(name) + '\n'; result.insert(0, thisResult); } output.print(result.toString()); // OUTPUT METHOD 2 // Output in reverse alphabetic order: TreeMap has a method that // returns the key set in descending order (reverse alphabetic order) output.println(); output.println("Output using descendingKeySet"); for (String name : goalsScored.descendingKeySet()) { String thisResult = name + ":" + goalsScored.get(name); output.println(thisResult); } } }