aspose file tools*
The moose likes Beginning Java and the fly likes how does the hasNextInt() work in the following situation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does the hasNextInt() work in the following situation? " Watch "how does the hasNextInt() work in the following situation? " New topic
Author

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

Pratheek Baliga
Greenhorn

Joined: Jul 29, 2012
Posts: 2


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

Joined: Mar 17, 2011
Posts: 7063
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jul 29, 2012
Posts: 2
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
You’re welcome
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Mar 10, 2008
Posts: 1666
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how does the hasNextInt() work in the following situation?
 
Similar Threads
returning to a catch block
comparing an element in one array with all the elements in another array
Two Dimensional array declaration in Java
Class and Object Problem
Exception Handling