I have this following input.
3
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
mark
john
and this is my code
public static void main(String[] argh) {
Map<String, Integer> map = new HashMap<String, Integer>();
List<String> keyList, outputList = new LinkedList<String>();
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for (int i = 0; i < N; i++) {
String name = in.next();
int phone = in.nextInt();
in.nextLine();
map.put(name, phone);
}
keyList = new ArrayList<String>(map.keySet());
String s = in.nextLine();
while (in.hasNext()) {
if (keyList.contains(s)) {
outputList.add(s + "=" + map.get(s));
} else {
outputList.add("Not found");
}
s = in.nextLine();
}
in.close();
for (int i = 0; i < outputList.size(); i++) {
System.out.println(outputList.get(i));
}
}
My problem is that I could not determine whether the input has ended or not because while (in.hasNext()) {
freezes after the last input. How could I close the scanner after the last input?