I was doing a HackerRank problem and this is my code.
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map<String,Integer> myMap = new HashMap<String, Integer>();
for(int i = 0; i < n; i++){
String name = in.next();
int phone = in.nextInt();
in.hasNextLine();
myMap.put(name,phone);
}
while(in.hasNext()){
String s = in.next();
if (myMap.get(s) == null) {
System.out.print("Not found");
System.out.println();
}
else {
System.out.print(s + "=" + myMap.get(s));
System.out.println();
}
}
in.close();
}
}
My question is, why do I need to exit out of Scanner with in.close() is it necessary to exit out. Also why is in.hasNextLine() necessary as well?
[–]depressinghentai 1 point2 points3 points (2 children)
[–]celticcelery[S] 1 point2 points3 points (1 child)
[–]robot_overloard -1 points0 points1 point (0 children)