import java.util.*; import java.io.*; public class TopCities { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(); Scanner scan = null; try { scan = new Scanner(new File(args[0])); } catch (Exception fnfe) { System.err.println("Usage: java TopCities filename"); System.exit(-1); } String name, pop; while (scan.hasNext()) { name = scan.next(); System.out.println(name); pop = scan.next(); while (!Character.isDigit(pop.charAt(0))) { // it was another word! // deal with possible space in city name pop = scan.next(); } // at this point pop is a sring beginning with a digit // but perhaps containing commas: deal with them! // by making popInt be a proper int with no commas pq.offer (new CityPop(name, popInt)); } CityPop foo; for (int i = 0; i<10; i++) { foo = pq.poll(); System.out.println((i+1)+": "+foo.getName()+" "+foo.getPop()); } } }