package pl.codegym.task.task10.task1019;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/*
Funkcjonalność to nie wszystko!
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> map = new HashMap<String, Integer>();
int id = 0;
String imie = null;
while (true) {
String sId = reader.readLine();
if (sId == null)
break;
id = Integer.parseInt(sId);
imie = reader.readLine();
if (imie == null)
break;
map.put(imie, id);
}
if (imie == null)
map.put("", id);
for (Map.Entry<String, Integer> para : map.entrySet()) {
imie = para.getKey();
id = para.getValue();
System.out.println("Id=" + id + " Imię=" + imie);
//System.out.println(id + " " + imie);
}
}
}
Right output, no validation
Rozwiązane
Komentarze (4)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Enid
23 kwietnia 2020, 14:55
I had a very similar solution but struggled with the last requirement just like you. Although what the program prints looked like the correct output, my program wouldn't stop after passing an empty string. I could pass another pair after that and it would override the first pair in the map.
I conclude what makes a change is the last
that is outside the while loop. Am I right?
I still don't understand why we can't add that pair (map.put("", id)) followed up by the break; inside the while loop. Why the program keeps running? 0
Misiu
23 kwietnia 2020, 16:07
It is possible to remove last IF.
Try this:
if (imie.equals("")) { //empty string
map.put(imie, id);
break;
}
map.put(imie, id);
0
Guadalupe Gagnon
14 lutego 2020, 20:18rozwiązanie
https://codegym.cc/help/6910
+4
Misiu
14 lutego 2020, 23:24
sId.equals("") gave me the right solution.
Also I had to uncomment last line of my code and comment the line before (the line given by CodeGym). I wonder if that was intencional...
Thank you Master :)
+1