Advertisement
Guest User

Untitled

a guest
May 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedHashMap;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created by Zdravko on 5/30/2016.
  7.  */
  8. public class PopulationCounter {
  9.     public static void main(String[] args) {
  10.         Scanner scan = new Scanner(System.in);
  11.         HashMap<String, HashMap<String, Integer>> countries = new HashMap<String,HashMap<String,Integer>>();
  12.         String commands = scan.nextLine();
  13.  
  14.         while (!commands.equals("report")) {
  15.             String[] input = commands.split("\\|");
  16.             String city = input[0];
  17.             String country = input[1];
  18.             int population = Integer.parseInt(input[2]);
  19.  
  20.             if(!countries.containsKey(country)){
  21.                 countries.put(country, new LinkedHashMap<String,Integer>());
  22.             }
  23.  
  24.             if(!countries.get(country).containsKey(city)){
  25.                 countries.get(country).put(city, 0);
  26.             }
  27.  
  28.             int sum = countries.get(country).get(city) + population;
  29.             countries.get(country).put(city,sum);
  30.             commands = scan.nextLine();
  31.         }
  32.         commands = scan.nextLine();
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement