Created
July 18, 2016 13:59
-
-
Save josinSbazin/ca2d1716b18676ba954d6ad0eeace646 to your computer and use it in GitHub Desktop.
level19.lesson10.home01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level19.lesson10.home01; | |
/* Считаем зарплаты | |
В метод main первым параметром приходит имя файла. | |
В этом файле каждая строка имеет следующий вид: | |
имя значение | |
где [имя] - String, [значение] - double. [имя] и [значение] разделены пробелом | |
Для каждого имени посчитать сумму всех его значений | |
Все данные вывести в консоль, предварительно отсортировав в возрастающем порядке по имени | |
Закрыть потоки. Не использовать try-with-resources | |
Пример входного файла: | |
Петров 2 | |
Сидоров 6 | |
Иванов 1.35 | |
Петров 3.1 | |
Пример вывода: | |
Иванов 1.35 | |
Петров 5.1 | |
Сидоров 6.0 | |
*/ | |
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
if (args.length<1) return; | |
File file = new File(args[0]); | |
if (!file.exists()) return; | |
Map<String, Double> map = new TreeMap<String, Double>(); | |
// BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "CP1251")); | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
while (reader.ready()) { | |
String line = reader.readLine(); | |
if (!line.equals("")) { | |
String[] values = line.split(" "); | |
String key = values[0]; | |
Double value = Double.parseDouble(values[1]); | |
if (map.containsKey(key)) map.put(key, map.get(key)+value); | |
else map.put(key, value); | |
} | |
} | |
reader.close(); | |
// List <Map.Entry<String, Double>> entries = new ArrayList<Map.Entry<String, Double>>(map.entrySet()); | |
// Collections.sort(entries, new Comparator<Map.Entry<String, Double>>() { | |
// @Override | |
// public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) { | |
// return o1.getKey().length() - o2.getKey().length(); | |
// } | |
// }); | |
// | |
// for (Map.Entry<String, Double> m : entries) { | |
// System.out.println(m.getKey()+" "+m.getValue()); | |
// } | |
for (Map.Entry<String, Double> m : map.entrySet()) System.out.println(m.getKey()+" "+m.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment