Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
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)); } }
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
0
Thank you very much!! That was the problem. :)