+ 1

Why scanner object dont read last line of input

For input value of "World" in cc and "Hello" for c, System.out.println(c + cc); only prints Hello https://code.sololearn.com/c4pz871dBB5F/?ref=app

20th Jan 2021, 6:29 PM
Shreyansh
Shreyansh - avatar
5 Antworten
+ 6
Shreyansh Do like this then it will read. String cc = null; if(scan.nextLine() != null) cc = scan.nextLine(); Or you can just use next() method like String cc = scan.next();
20th Jan 2021, 6:45 PM
A͢J
A͢J - avatar
+ 2
Pąľľąvī Yes there is another method next (). Shreyansh There is a problem with nextLine () method. After using nextInt() or nextDouble(), nextLine method starts reading from the cursor's current position so after reading the value using nextInt or nextDouble, cursor just start reading after the int or double value which will be new line '\n'. That's why cc is just '\n' so you have check like I told you.
20th Jan 2021, 7:07 PM
A͢J
A͢J - avatar
+ 1
Thank you I Am AJ !, It really helped a lot 😊
21st Jan 2021, 2:39 AM
Shreyansh
Shreyansh - avatar
0
Pąľľąvī There is no nextWord in Scanner class.
20th Jan 2021, 6:47 PM
A͢J
A͢J - avatar
0
Yes exactly Pąľľąvī , But when it comes to read a line, problem begins
21st Jan 2021, 2:41 AM
Shreyansh
Shreyansh - avatar