• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to understand Scanner working  RSS feed

 
thejwal pavithran
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




in the above code, i get the following outputs as below :

enter an integer
1
enter an integer
wwwww
STOP IT! enter an integer
STOP IT! enter an integer


what i dont get is, why is the message " STOP IT! enter an integer" getting printed twice for invalid inpt..what is my mistake?please help
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to use the Scanner#next() method in preference to nextLine() there.
What happens is that you read xxx and recognise that as not an int. Then you think nextLine reads the next line, as even some books mistakenly tell you, but it doesn’t. It reads the remainder of the line, which is probably an empty String. Then it recognises that as not an int, and the app prints your error message again.
Another, related, problem you can get with nextLine to be found here.
 
thejwal pavithran
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: It reads the remainder of the line, which is probably an empty String. Then it recognises that as not an int, and the app prints your error message again.


I don't get it, which is the empty string that is read?is this how it is ? : suppose i type in www, the hasNextInt() recognizes that it is not an 'int' that will be read next and goes on to print the error message. after that www is read and ignored. then there is an empty line (remainder which comes after www) which again leads to printing the error message..
documentation of nextLine() :

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.


i went through the above lines few times but i should admit im still not understanding the working.i mean,when sc.nextLine() is used, the whole line is read or only the text is read?


and this output below is further confusing me:

enter an integer
ww
STOP IT! enter an integer


the scanner prints twice the error message only if the invalid input is followed by a valid input..otherwise works fine..

scanner.next() reads till a space as i know so if a user types two or more words, the message would be printed that many number of times isnt it?

so can i get a simple explanation on how this should be corrected an also the difference between next() and nextLine()?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the link I posted in my earlier post? Did that post not explain it?
There is not an empty line. The nextLine() method reads from its current position to the end of the line. It says it quite clearly in the text you quoted: “returns the rest of the current line”. It does not go to the next line, even though some books will tell you that.I have reposted your code, with alterations to lines 14-15. I have also got rid of the blank lines, which do not help legibility, and the comments. Use 4 spaces, not tabs, for indentation. Names like ch and ss are poor style, because nobody reading the code can work out what they mean.
Now you can see the exact text you are reading. You should see it will print “” as the remainder of the line after xxx. Try changing the line to
xxx        ↩
… and the printout will look like this
Not an integer: you entered “xxx”.
Not an integer: you entered “        ”.
…when sc.nextLine() is used, the whole line is read or only the text is read?
Neither nor. I have already told you what it reads.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!