My output is correct, but fail the last condition, please help.
package com.codegym.task.task19.task1919;
/*
Calculating salaries
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
HashMap<String, Double> map = new HashMap<>();
String fileName = args[0];
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while (bufferedReader.ready()) {
String[] strArr = bufferedReader.readLine().split(" ");
String name = strArr[0];
double d = Double.parseDouble(strArr[1]);
if (map.containsKey(name)) {
map.replace(name, map.get(name) + d);
} else {
map.put(name, d);
}
}
fileReader.close();
bufferedReader.close();
Map<String, Double> map1 = sortByValue(map);
for (Map.Entry<String, Double> en : map1.entrySet()) {
System.out.println(en.getKey() +
" " + en.getValue());
}
}
public static HashMap<String, Double> sortByValue(HashMap<String, Double> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Double> > list =
new LinkedList<Map.Entry<String, Double> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Double> >() {
public int compare(Map.Entry<String, Double> o1,
Map.Entry<String, Double> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Double> temp = new LinkedHashMap<String, Double>();
for (Map.Entry<String, Double> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
}