Dear CodeGymMember,
Requirement is not clear to me, will it be possible to explain it more?
Thank you.
Have a nice day,
Sanjay Chauhan
package com.codegym.task.task19.task1920;
/*
The richest
The first parameter of the main method is a file name.
Each line of the specified file has the following format:
name value
where [name] is a String and [value] is a double. [name] and [value] are separated by a space.
Find the sum of all the values for each name.
Display in alphabetical order the names with the highest sum.
Separate the names with a space or new line.
Close the streams.
Example input file:
Peterson 0.501
Smith 1.35
Peterson 0.85
Example output:
Peterson
Requirements:
1. The program must NOT read data from the console.
2. The program must read the file's contents (use FileReader).
3. The file input stream (FileReader) must be closed.
4. The program should output the names with the highest sum to the console.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) throws IOException {
SortedMap<String,Double> map = new TreeMap<>();
FileReader reader = new FileReader(args[0]);
BufferedReader bufferedReader = new BufferedReader(reader);
while(bufferedReader.ready()){
String[] line = bufferedReader.readLine().split(" ");
map.merge(line[0],Double.parseDouble(line[1]),Double::sum);
}
Double max = map.values().stream().max(Double::compare).get();
for(Map.Entry<String,Double> map1 : map.entrySet() ){
if(map1.getValue()==max)
System.out.println(map1.getKey());
}
bufferedReader.close();
reader.close();
}
}