• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Scanner problem  RSS feed

 
sinatra roger
Ranch Hand
Posts: 127
  • 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?
 
Jeff Verdegan
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: 127
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!