• Post Reply Bookmark Topic Watch Topic
  • New Topic

Failing to read input with Scanner#nextLine  RSS feed

 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

I'm still not able to understand the Scanner issue here. I'm trying to do that you advised earlier. Just made a simple code, but in any case it's not taking the input I'm trying to enter for the city.
Scanner.PNG
[Thumbnail for Scanner.PNG]
Scanner Issue
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also attaching the scanner inputs that I'm submitting but at the last one I enter a name but it doesn't show up.
ScannerOuput.PNG
[Thumbnail for ScannerOuput.PNG]
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:it's not taking the input I'm trying to enter for the city.

That's because you've commented out the line that assigns the value returned from the scanner.nextLine() call to a variable.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stuart, that works. But I'm not able to figure why? Also I think I finally understood what Ritchie meant and modified the code and it works. So I used input.nextLine() just after input.nextInt() to discard the value that the nextInt() is taking.

But as Stuart suggested I put the line of code back into the snippet and that works too, any explanation on why does that work? Or maybe I'm missing out something here.

Thanks,
Aakash
Scanner1.PNG
[Thumbnail for Scanner1.PNG]
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote: . . . That's because you've commented out the . . .
I don't think that is the explanation.

Aki Mohan I am not sure you should be adding new questions to an old thread. I shall create a new thread.
Please don't post screenshots; they are difficult to read and search. How do you get green text? I only remember seeing green text on Eclipse. What does it mean that there is no text? Are you not entering the name of the city, or does it fail to appear when you print it out?

I may have posted something in the old thread relevant to your use of Scanner.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the old post I mentioned. Yes, after nextXXX() you probably have to read nextLine() and discard the value before you can use anything useful. I think you have worked it out, but those screenshots are so difficult to read with that coloured text.

I have my own ideas about Scanner. Put any scanners pointing to System.in into a utility class and use that for all keyboard input. You can read about nextLine in the previous post and here, but beware of the daft mistake.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I shouldn't have raised the question in another thread and also sorry for those screenshots. Yeah that's from Eclipse

Thanks,
Aakash
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!