+ 2

How to read several values from console in java?

I need to read a string, integer number and one more string. Then I want to work with this data. I use the next code: Scanner in = new Scanner(System.in); String str1 = in.nextLine(); int a = in.nextInt(); String str2 = in.nextLine(); But after inputting second line (int a) my programm skips inputting str2. Why? How can I fix it?

11th Feb 2020, 3:48 PM
Dmitriy Polyakov
Dmitriy Polyakov - avatar
2 Respostas
+ 2
I have fixed it. I changed the line "int a = in.nextInt();" to the next one "int a = Integer.parseInt(in.nextLine());". It works correctly now. But I don't know why. Can somebody explain ?
11th Feb 2020, 3:55 PM
Dmitriy Polyakov
Dmitriy Polyakov - avatar