• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner's nextDouble() method -- returning an integer?  RSS feed

 
Alexander Alvonellos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Given the following code

If I pass in "1 9.0" as input, it will say "Generally not felt by people" which is a message calcuated in another class not pertinent to this post. My question is, why does nextDouble() pass back 1 as a parameter, when it is technically not a double. If nextDouble() can pass back an integer and a double, is there a way to specify that nextDouble() MUST return a floating point number?

Thanks,
-- Alex
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that '1' is parsed as a floating point number when you ask a scanner to parse the next thing as a floating point number, would generally be considered wanted behaviour.

Why do you want to force your user to enter '1.0' instead of just '1'?
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander,

Welcome to JavaRanch!
 
Alexander Alvonellos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The fact that '1' is parsed as a floating point number when you ask a scanner to parse the next thing as a floating point number, would generally be considered wanted behaviour.

Why do you want to force your user to enter '1.0' instead of just '1'?


While I understand that it is generally desired to simply parse the next number as a floating point number, I would like to know how I would select only a floating point number out of a list of possible values, as would be the case in some of my programming team practice challenges. Now that I understand that nextDouble() simply returns the next value in Double form, I would like to know how I would accomplish the described behavior above, if I so please.

Thanks!
 
Alexander Alvonellos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:Alexander,

Welcome to JavaRanch!


Thanks for the warm welcome! I surely appreciate it!
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could keep calling hasNextInt() and nextInt(), until hasNextInt() no longer returns true. Then check if hasNextDouble() is true.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, you could keep calling hasInt() . . . hasDouble() . . .
Shouldn't that read hasNextInt() and hasNextDouble()?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I had already fixed it after I took a quick look at the API. It's been a while since I used Scanners :P
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That way you filter out specific input while letting through anything else. To be specific: If hasNextInt() is false, the next token could still be text. So if you don't want to read next() and check and parse the token yourself, you have to loop over next() until (hasNextDouble() && !hasNextInt()).
 
Alexander Alvonellos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:That way you filter out specific input while letting through anything else. To be specific: If hasNextInt() is false, the next token could still be text. So if you don't want to read next() and check and parse the token yourself, you have to loop over next() until (hasNextDouble() && !hasNextInt()).


Brilliant! Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!