• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

java.util.Scanner problem

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


hey guys please see the line marked with "see this line in the above code". i added this line without actually knowing what i was doing. i mean i figured out that one line was being read in without entering any line inside of the for loop which follows..so i just read that blank line outside the loop so that the user would be able to the correct number of lines...can somebody please explain why exactly a blank line is being read here?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nextLine() reads from the current position up through the next end of line character. If you did something like nextInt() right before that, then the nextInt() will consume the characters that make up the int, but it doesn't consume the EOL characters at the end of that line, so you need the nextLine() to do that.
 
sinatra roger
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i got it so just to confirm : below i am reading an int value but there is an unread EOL value..which is read in by the in.nextLine(). is my thinking right?
if so, is this the proper way to read this EOL character?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thejwal pavithran wrote:i think i got it so just to confirm : below i am reading an int value but there is an unread EOL value..which is read in by the in.nextLine(). is my thinking right?
if so, is this the proper way to read this EOL character?



Yes, I believe that's correct, and I think that's the standard way of handling these cases with Scanner. I never use Scanner myself.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic