Why is this code not showing the name | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this code not showing the name

can u correct it?? https://code.sololearn.com/cGYS8B8Yzm7D/?ref=app

15th Jan 2018, 1:10 PM
Arun
7 Answers
+ 10
Just enter the age and name in the same line of input, you need not input in two different (or separate) lines. Or if you really want to input in two separate lines, just use next() instead of nextLine()
15th Jan 2018, 2:04 PM
Dev
Dev - avatar
+ 8
Try next() instead of nextLine()
15th Jan 2018, 2:04 PM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
+ 5
The reason is the way of Java handle input, and particulary nextInt() function: it try to get only valid character (digits) which form an integer number, and leave the input stream as is, without getting the rest of line (even if the rest of line is only the new line character)... To fix it, you can modify your way of giving input... instead of: 42 Toto ... it will work with: 42 Toto ... but to correct the code and have the expected behaviour, you need to do twice the nextLine() call: String y=sc.nextLine(); y=sc.nextLine();
15th Jan 2018, 2:12 PM
visph
visph - avatar
+ 3
You're able to update it yourself: if you doesn't, you will not learn ^^
15th Jan 2018, 3:45 PM
visph
visph - avatar
+ 3
because String is required only on declaration of variable... once it's done, you doesn't have to redeclare it.
15th Jan 2018, 9:08 PM
visph
visph - avatar
+ 3
I have too one question: why set twice the best answer mark on my posts, and immediatly remove it? Nothing against @Dev, but I think that I have deeply explain, while @Dev just give a solution ^^
15th Jan 2018, 9:12 PM
visph
visph - avatar
- 1
thanks everyone Last question why writing string y=sc.nextLine(); twice produce corrected output??
15th Jan 2018, 9:03 PM
Arun