I have been at this for a day, it works, matches the initial criteria, however the error I keep getting is that I've mixed up the input order.
I rectified this by using a LinkedHashmap and cloning it back to the originally declared HashMap variable.
I know it needs to read the pairs into this HashMap table directly, however it changes the order from order of user input, to order by String Hash key value.
I know why it's failing, I just don't know how to resolve it. Any pointers would be appreciatedpackage com.codegym.task.task10.task1019;
import java.io.*;
import java.util.*;
/*
Functionality is not enough!
*/
public class Solution {
public static void main(String[] args) throws IOException {
HashMap<String, Integer> map2 = new HashMap<>();
LinkedHashMap<String, Integer> map1 = new LinkedHashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
while (true) { //name is blank
int id = Integer.parseInt(reader.readLine());
String name = reader.readLine();
map1.put(name, id);
if (name.isEmpty()) {
map1.remove(name, id);
break;
}
}
}
catch (NumberFormatException r) {
while (true) { //id is blank
try {
Integer id = Integer.parseInt(reader.readLine());
String name = reader.readLine();
map1.put(name, id);
} catch (NumberFormatException s) {
break;
}
}
}
map2 = (HashMap<String, Integer>) map1.clone();
for (Map.Entry<String, Integer> map : map2.entrySet()) {
String index = map.getKey();
Integer value = map.getValue();
System.out.println( index + " " + value);
}
}
}