wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes cannot read double input from keyboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot read double input from keyboard" Watch "cannot read double input from keyboard" New topic
Author

cannot read double input from keyboard

Clay Bobby
Greenhorn

Joined: Jul 03, 2009
Posts: 3
Ok, I have searched on this site, google, and I have skimmed the documentation for the Scanner... and still cannot figure out what is wrong with this. I have simplified the offending code:



No matter what I do, I cannot read any input with decimals. I can only input integers into this. If I try something like 3.11225 or even 1.1, I get this junk:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at decisions.main(decisions.java:11)



I am running OS X 10.4. What is going on?

Thanks,
Clay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to JavaRanch

Don't know. I tried your quoted code on Ubuntu/JDK6u14 and it worked first time.
Clay Bobby
Greenhorn

Joined: Jul 03, 2009
Posts: 3
Thanks for the welcome.

So you put a decimal in it and it worked? This is driving me up the wall.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

I tried it as well, and found out that Scanner is locale sensitive. You probably use , as the decimal separator, just like me. Using 1,1 will work. I added "scan.useLocale(Locale.US);" before the call to nextDouble, and then 1.1 does work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Clay Bobby
Greenhorn

Joined: Jul 03, 2009
Posts: 3
I am learning Spanish right now and have switched my system over to EspaƱol. Works fine with the comma.

LOL, thanks.

-Clay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Ole
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot read double input from keyboard