import java.io.PrintStream; import java.util.Scanner; import java.util.Collections; import java.util.Map; import java.util.TreeMap; import java.util.StringTokenizer; public class GamesWon { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); Map results = new TreeMap(); output.println("Enter scores for each game (done to finish)"); String line = input.nextLine(); while (!line.equalsIgnoreCase("done")) { StringTokenizer tok = new StringTokenizer(line); try { String team1 = tok.nextToken(); String team2 = tok.nextToken(); int goals1 = Integer.parseInt(tok.nextToken()); int goals2 = Integer.parseInt(tok.nextToken()); // figure out the winning team String winner = team1; String loser = team2; if (goals1 < goals2) { winner = team2; loser = team1; } // update the map with the winner's result Integer gamesWon = results.get(winner); if (gamesWon == null) { gamesWon = 0; } results.put(winner, gamesWon + 1); // update the map with the loser's result gamesWon = results.get(loser); if (gamesWon == null) { results.put(loser, 0); } } catch (RuntimeException ex) { output.println("Bad score; please try again."); } line = input.nextLine(); } // output results output.println(); output.println("Games won by team"); Integer maxPoints = Collections.max(results.values()); for (String team : results.keySet()) { Integer points = results.get(team); String pString = ""; if (points.equals(maxPoints)) { pString = "*"; } output.printf("%10s %3s%s%n", team, points.toString(), pString); } } }