OlympicGames - catch (ArrayIndexOutOfBoundsException e) {

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.io.*; import java.util.*; i public class OlympicGames { public static void main(String[] args) { PrintStream out = System.out; Scanner in = new Scanner(System.in); final int FOUR = 4; Map<String, Integer[]> nations = new HashMap<String, Integer[]>(), sort; String buffer; while (!(buffer = in.nextLine()).equals("done")) { try { if (buffer.split(",").length == FOUR) { buffer = buffer.substring(buffer.indexOf(",") + 1); int i = 0; for (String index : buffer.split(",")) { Integer medals[] = {0, 0, 0}; index = index.trim(); medals = nations.containsKey(index) ? nations.get(index) : medals; medals[i] += 1; nations.put(index, medals); i++; } } else { throw new IndexOutOfBoundsException(""); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: catch (ArrayIndexOutOfBoundsException e) { out.println("Missing fields!"); } } List<Integer> goldMedal = new ArrayList<Integer>(); for(Integer medalType : nations.values()) { goldMedal.add(medalType[0]); } Integer highest = Collections.max(goldMedal); while (!nations.isEmpty()) { sort = new TreeMap<String, Integer>(); for (String nation : nations.keySet()) { if (nations.get(nation)[0] == highest) { sort.put(nation, nations.get(nation)); } } for (String nation : sort.keySet()) { Integer medals = sort.get(nation); out.printf("%25s%3d%3d%3d%n", nation, medals[0], medals[1], medals[2]); nations.remove(nation); } highest -= 1; } } }...
View Full Document

This note was uploaded on 09/16/2011 for the course CSE CSE 1020 taught by Professor Burton,ma during the Fall '10 term at York University.

Page1 / 2

OlympicGames - catch (ArrayIndexOutOfBoundsException e) {

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online