Advertisement
shady_obeyd

07.Sums by Town

Dec 4th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class SumsByTown {
  7.     public static void main(String[] args){
  8.         Scanner s = new Scanner(System.in);
  9.  
  10.         int n = s.nextInt();
  11.  
  12.         TreeMap<String, Double> towns = new TreeMap<>();
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String[] tokens = s.nextLine().split("\\|");
  16.             String town = tokens[0].trim();
  17.             double value = Double.parseDouble(tokens[1].trim());
  18.  
  19.             if(towns.containsKey(town)){
  20.                 towns.put(town, towns.get(town) + value);
  21.             }
  22.             else {
  23.                 towns.put(town, value);
  24.             }
  25.         }
  26.  
  27.         for (String town : towns.keySet()) {
  28.             System.out.println(String.format("%s -> %f", town, towns.get(town)));
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement