Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how does the hasNextInt() work in the following situation?

 
Pratheek Baliga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello,
Can somebody tell me how does the control pass through the if( sc.hasNextInt()) for the first time when nothing has been entered from the keyboard?
The buffer would contain nothing isn't it? So what would the hasNextInt() return?

Regards,
Pratheek
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratheek Baliga wrote:Can somebody tell me how does the control pass through the if( sc.hasNextInt()) for the first time when nothing has been entered from the keyboard?
The buffer would contain nothing isn't it? So what would the hasNextInt() return?

Basic answer: false; and furthermore I don't think it blocks while waiting for input (the API documentation certainly doesn't mention it). I believe you use hasNext() in order to ensure that the program blocks until it receives some input.

However, what you've written won't actually do anything, since your loop control variable is set to the wrong value (or your loop condition is wrong).

Personally, I'm not a huge fan of Scanner, and I find its API quite counter-intuitive (it's also quite slow).

If I need to use one, I generally use nextLine() and then check what I've got; but that's a cop-out in terms of your question.

Only other advice I have is: test, test, test .

Winston
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unlike Winston, I quite like Scanner. But he is right about nextLine(). You can even buy books which tell you nextLine returns the next line, which is quite wrong. Lots of other people get the same confusion, as seen here, for example.
Don’t use == false or == true. They are poor style and error‑prone. It is while (!b)..., not while (b == false)...
This is a far better version of that utility method
 
Pratheek Baliga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys. Got my doubt cleared. Also will try to better my coding style.
Loved the fact that you responded quickly.

Cheers!
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pratheek Baliga wrote:Can somebody tell me how does the control pass through the if( sc.hasNextInt()) for the first time when nothing has been entered from the keyboard?
The buffer would contain nothing isn't it? So what would the hasNextInt() return?

Basic answer: false; and furthermore I don't think it blocks while waiting for input (the API documentation certainly doesn't mention it). I believe you use hasNext() in order to ensure that the program blocks until it receives some input.

The API certainly does mention blocking while waiting for input:
java.util.Scanner wrote:A scanning operation may block waiting for input.

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner's API documentation is very inconsistent in mentioning this, though.
The hasNext(Pattern), hasNext(String), hashNextInt(), hasNextInt(int) etc. methods don't state this possible blocking behavior or refer to other parts of the documentation in any way.
It could do with some editing.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you like, sure. But this is in the general part of the API, that applies to the class as a whole. Every single method in Scanner that requires a read of characters will block if the underlying stream blocks. Is it really necessary to put this info in each and every method? It's tedious and unnecessary, in my opinion, and the general comment makes it clear that the blocking occurs. It's not unusual for other classes to have comments like this that apply to whole groups of methods, containing information not noted on each individual method. It may not be as clear as we like, but the info is there, if we read it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic