Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner.nextline() Help  RSS feed

 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I need help on the usage of this line of code because i am not sure if is it the way i used it wrongly or is it that there is something i need to do to make it the way I want it or is it that i did it wrong.



the problem is that when ever i call the scn.nextline() the scanner will skip the input in the commondprompt and move on to the scn.nextInt() and when the loop goes in again, now then the scn.nextLine() worked can any one of you guys give me some suggestion to this problem or also can provide me the reason to how come this code work likes this. I have read the API but it is still no so clear to me and also i have try out some sample code but they all work find and the way they code it looks the same a mine i really hope than anyone of u can give me an explnation to this THANKS in advance.

regards
KKH
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you've seen in the API documentation, calling Scanner's nextLine method "advances this scanner past the current line and returns the input that was skipped." In this situation, you are using nextLine for 2 reasons: First, to capture the String the user enters (using the method's return value). And second, to simply advance to the next line (the method's "side effect"). I think the problem you're describing is from getting nextLine's side effect in the wrong spot.

Note the comments in the following revised code...

(Also note the effect of using print methods instead of println methods for the prompts.)
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi marc weber,

Thanks for the advice and it worked but i still dun really know what the extra scr.nextLine() in the end do to help the whole logic. Can u explain in more detaill ? THANKS in adv advance. I see the side effect form the src.nextLine() is there any way for us to avoid it??? Thank you.

regards
KKH
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im assuming this code is in a while or for loop,

you don't need the scn.nextLine(); at the end...

at all..

Justin
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Justin fox,

You said that the code don't need that line scn.nextLine() and u assume that the code is in a while loop or for loop. And you are correct the code is in a while loop but when i take out that line the program go back to the same problem and when i put it back the problem is sloved. Do you guys know why this happen and how to you avoid the side effect.

regards
KKH.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this loop, after getting nextInt, you definitely need the nextLine call at the end. It's not there to get any input. It's only there for the side effect of advancing to the next line.

If the nextLine call were not there, then nextInt would get the first int entered, but the Scanner would remain on that line. Then at the top of the loop (next iteration), it would read in an empty String (from where it left off after the last int, but before the line separator) and assign this empty String to the next name, then immediately go on to ask for the next mark.

Try the following code as is, and then with the nextLine call at the end of the loop commented out...

To really see what's happening, comment out the nextLine call, then try the following input:

[ July 21, 2006: Message edited by: marc weber ]
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi marc weber,

Thanks man thank you for the great help i got it now. Thank you very much.

regards
KKH
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you really "need" it all that bad?

couldn't you just use println instead?

the .nextLine() that is?

Justin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!