Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level19.lesson10.home02;
- /* Самый богатый
- В метод main первым параметром приходит имя файла.
- В этом файле каждая строка имеет следующий вид:
- имя значение
- где [имя] - String, [значение] - double. [имя] и [значение] разделены пробелом
- Для каждого имени посчитать сумму всех его значений
- Вывести в консоль имена, у которых максимальная сумма
- Имена разделять пробелом либо выводить с новой строки
- Закрыть потоки. Не использовать try-with-resources
- Пример входного файла:
- Петров 0.501
- Иванов 1.35
- Петров 0.85
- Пример вывода:
- Петров
- */
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- BufferedReader fileReader=null;
- try
- {
- fileReader = new BufferedReader(new FileReader(args[0]));
- String textLine="";
- ArrayList<String[]> goodLines = new ArrayList<String[]>();
- String []parsedLine;
- TreeSet<String> names = new TreeSet<String>();
- while ((textLine=fileReader.readLine())!=null) {
- try
- {
- parsedLine=textLine.split(" ");
- if (parsedLine.length==2) {
- goodLines.add(parsedLine);
- names.add(parsedLine[0]);
- }
- }
- catch (Exception e) {}
- }
- fileReader.close();
- TreeMap<String, Double> map = new TreeMap<String, Double>();
- for (String s : names) {
- double salary=0;
- for (String[]items : goodLines) {
- if (s.equals(items[0])){
- try
- {
- salary=salary+Double.parseDouble(items[1]);}
- catch (Exception e) {}
- }
- }
- map.put(s,salary);
- }
- double max = map.firstEntry().getValue();
- for (Map.Entry<String,Double> entry : map.entrySet()) {
- if (Double.compare(max,entry.getValue())<1){
- max=entry.getValue();
- }
- }
- for (Map.Entry<String,Double>entry: map.entrySet()) {
- if (Double.compare(entry.getValue(),max)==0) {
- System.out.println(entry.getKey());
- }
- }
- }
- catch (IOException e) {
- //System.out.println(e.getMessage());
- }
- /*------------------------*/
- finally
- {
- if (fileReader!=null) {
- try
- {
- fileReader.close();
- }
- catch (Exception e) {}
- }
- }
- }
- /*------------------------*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement