I can't understand where is the error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't understand where is the error

When I use nextInt to read the input as int I recibe the error, but if I change the variable"a" in the last line with 0 the Code run. import java.util.HashMap; import java.util.Scanner; public class datass{ HashMap<Integer,String> points = new HashMap<Integer,String>(); public String search_in(int number){ return points.get(number); } public void add_row(int a,String name){ points.put(a,name); } } public class MyClass { public static void main(String[ ] args) { datass id_person = new datass(); Scanner pal = new Scanner(System.in); Scanner num = new Scanner(System.in); String words = pal.nextLine(); id_person.add_row(0,words); int a = num.nextInt(); System.out.println(id_person.search_in(a)); } }

2nd Jul 2019, 6:36 PM
Pablo Díaz Garrido
Pablo Díaz Garrido - avatar
2 Answers
+ 1
it is not a good idea to use two scanners for System.in (it produces errors), use one for it Scanner in = new Scanner(System.in); //Scanner num = new Scanner(System.in); String words = in.nextLine(); id_person.add_row(0,words); int a = in.nextInt(); keyboard input: Pablo 0 Output: Pablo
2nd Jul 2019, 7:25 PM
zemiak
0
Thank you very much!! That was the problem. :)
2nd Jul 2019, 7:35 PM
Pablo Díaz Garrido
Pablo Díaz Garrido - avatar