0

Scanner skipping-bypassing second input.

Why is my Scanner by-passing the the second input? Code is part of a larger project...so this code is just to explain the issue I have.. Scanner input = new Scanner(System.in); int myint = input.nextInt(); System.out.println(myint); String mystring = input.nextLine(); // <why is this line.... System.out.println(mystring); // and this line being skipped?

16th Nov 2019, 10:08 PM
rodwynnejones
rodwynnejones - avatar
6 Respuestas
+ 9
rodwynnejones Hello, Can you share your code with us. On SL, the input works differently. You must enter everything before running the code. For example, Enter a number and the string into separate lines: 2019 hello Than press SUBMIT
16th Nov 2019, 11:30 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 2
Martin Taylor is correct. As a suggestion, instead of adding an extra input.nextLine() call, using something like int myInt = Integer.parseInt(input.nextLine()) will likely express the intention more clearly, rather than having a reader think "why is there this random call here, is it a bug?"
17th Nov 2019, 5:42 AM
LunarCoffee
LunarCoffee - avatar
0
Hi. Tried it both in an IDE and code playground (and i an aware of the playground input method). If i do mystring.next()...it works fine, but that only allows me to input one "word" but I want to input a "phrase". The only way I got round in was to do two Scanner object e.g Scanner int_input = new Scanner(System.in); Scanner str_input = new Scanner(System.in); but I'm sure I should be able to do it using one.
16th Nov 2019, 10:58 PM
rodwynnejones
rodwynnejones - avatar
0
Not sure if this link will work.. but here goes. I've commented out the workaround i did to actually get it to work and put in the code that was causing the "issue". https://code.sololearn.com/c3X5T5dnjPYh/#java
16th Nov 2019, 11:47 PM
rodwynnejones
rodwynnejones - avatar
0
ah.... o.k thank you everyone who replied, I did think it had something to do with "something left in the buffer", I just didn't know how to "clear it". With your suggestion it now work as intended. (I see now why python was invented, LOL).
17th Nov 2019, 7:38 AM
rodwynnejones
rodwynnejones - avatar