• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74380
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74380
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic