Advertisement
jaVer404

level19.lesson10.home01(Sorted by value)

Feb 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package com.javarush.test.level19.lesson10.home01;
  2.  
  3. /* Считаем зарплаты
  4. В метод main первым параметром приходит имя файла.
  5. В этом файле каждая строка имеет следующий вид:
  6. имя значение
  7. где [имя] - String, [значение] - double. [имя] и [значение] разделены пробелом
  8.  
  9. Для каждого имени посчитать сумму всех его значений
  10.  
  11. Все данные вывести в консоль, предварительно отсортировав в возрастающем порядке по имени
  12. Закрыть потоки. Не использовать try-with-resources
  13.  
  14. Пример входного файла:
  15. Петров 2
  16. Сидоров 6
  17. Иванов 1.35
  18. Петров 3.1
  19.  
  20. Пример вывода:
  21. Иванов 1.35
  22. Петров 5.1
  23. Сидоров 6.0
  24. */
  25.  
  26. import java.io.*;
  27. import java.util.*;
  28.  
  29. public class Solution {
  30.     public static void main(String[] args) {
  31.         String fileName = null;
  32.         BufferedReader fileReader = null;
  33.         try
  34.         {
  35.             fileName=args[0];
  36.             fileReader = new BufferedReader(new FileReader(fileName));
  37.             String temp = "";
  38.             ArrayList<String[]> fileString = new ArrayList<String[]>();
  39.             HashSet<String> names = new HashSet<String>();
  40.             String []tempValue=null;
  41.             while ((temp=fileReader.readLine())!=null) {
  42.                 tempValue=temp.split(" ");
  43.                 names.add(tempValue[0]);
  44.                 fileString.add(tempValue);
  45.             }
  46.             fileReader.close();
  47.             HashMap<String,Double> namesAndDoubles = new HashMap<String, Double>();
  48.             for (String name : names) {
  49.                 double tempDouble = 0;
  50.                 for (String[]values : fileString) {
  51.                     if (name.equals(values[0])) {
  52.                         tempDouble=tempDouble+Double.parseDouble(values[1]);
  53.                     }
  54.                 }
  55.                 namesAndDoubles.put(name,tempDouble);
  56.             }
  57.  
  58. /*--------------------------------------------------------------------*/
  59.  
  60.     Object[] a = namesAndDoubles.entrySet().toArray();
  61.     Arrays.sort(a, new Comparator() {
  62.         public int compare(Object o1, Object o2) {
  63.             return ((Map.Entry<String, Double>) o1).getValue().compareTo(
  64.                     ((Map.Entry<String, Double>) o2).getValue());
  65.         }
  66.     });
  67.     for (Object e : a) {
  68.         System.out.println(((Map.Entry<String, Double>) e).getKey() + " "
  69.                 + ((Map.Entry<String, Double>) e).getValue());
  70.     }
  71.     }//end of try
  72.         catch (IOException e)
  73.         {
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement