Ideally it should be just one person, but when I try to verify the code it shows an error as display the names in ascending order which share the highest sum. So I tried to change the code to make it a list of people who has highest and same sum. but still not verifying and giving me an error as "be sure that the program displays names with the highest sum"
package com.codegym.task.task19.task1920;
/*
The richest
*/
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.*;
import java.util.HashMap;
import java.util.TreeMap;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Solution {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader(args[0]);
BufferedReader br = new BufferedReader(reader);
Map<String,Double> map = new TreeMap<>();
String line;
while((line = br.readLine()) != null) {
String[] lineArr = line.split(" ");
if(map.containsKey(lineArr[0].trim())){
map.put(lineArr[0].trim(), map.get(lineArr[0].trim()) + Double.parseDouble(lineArr[1]));
}else{
map.put(lineArr[0].trim(), Double.parseDouble(lineArr[1]));
}
}
reader.close();
br.close();
ArrayList<String> richestPerson = new ArrayList<>();
double maxValue = Integer.MIN_VALUE;
for(Map.Entry<String, Double> pair:map.entrySet()){
if(pair.getValue() >= maxValue){
maxValue = pair.getValue();
richestPerson.add(pair.getKey());
}
}
Collections.sort(richestPerson);
for(String name : richestPerson){
System.out.println(name);
}
}
}