0
Why is this code not showing the name
can u correct it?? https://code.sololearn.com/cGYS8B8Yzm7D/?ref=app
7 Respostas
+ 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()
+ 8
Try next() instead of nextLine()
+ 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();
+ 3
You're able to update it yourself: if you doesn't, you will not learn ^^
+ 3
because String is required only on declaration of variable... once it's done, you doesn't have to redeclare it.
+ 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 ^^
- 1
thanks everyone
Last question
why writing string y=sc.nextLine(); twice
produce corrected output??