aspose file tools
The moose likes Beginning Java and the fly likes Scanner.nextline() Help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Scanner.nextline() Help" Watch "Scanner.nextline() Help" New topic
Author

Scanner.nextline() Help

Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
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


if (!WorkHard){
System.out.println("Fail!!!");
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
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

Joined: Jan 24, 2006
Posts: 802
im assuming this code is in a while or for loop,

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

at all..

Justin


You down with OOP? Yeah you know me!
Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 05, 2005
Posts: 100
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

Joined: Jan 24, 2006
Posts: 802
do you really "need" it all that bad?

couldn't you just use println instead?

the .nextLine() that is?

Justin
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Scanner.nextline() Help
 
Similar Threads
<identifier expected> error
Grading Program..... (part 2)
Grading Program..... (part 2)
Loop through the list of objects to get attribute.
Are there structs in Java? Are they even called structs?