Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

java.util.Scanner problem  RSS feed

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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
  • 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: 146
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
ice is for people that are not already cool. Chill with this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!