I don't get what I'm doing wrong here. I have the code check if name is empty, if it is add to the hashmap "", id. Why am I getting number should also be displayed error?
package com.codegym.task.task10.task1019;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/*
Functionality is not enough!
*/
public class Solution {
HashMap<String, Integer> map;
static Integer id;
static String name;
public Solution() {
this.map = new HashMap<String, Integer>();
// map.put(index, name);
}
public static void main(String[] args) throws IOException {
Solution solution = new Solution();
HashMap<String, Integer> map = new HashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try{
while(true){ //continue the loop
String id = reader.readLine(); // read the number first
if(id.isEmpty()) break; // break if empty
String name = reader.readLine(); //read the string
if(name.equals("")){// check if empty
String emptyName = "";
int idSet = Integer.parseInt(id);
map.put(emptyName,idSet); // store the number mandatory with empty string
break; // but we still have to break it
}
map.put(name,Integer.parseInt(id)); // generic put statement for normal inputs
}
for (Map.Entry<String, Integer> pair : solution.map.entrySet()) {
name = pair.getKey();
id = pair.getValue();
System.out.println(id + " " + name);
}
}catch(Exception e){
}
}
}