This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot read double input from keyboard

 
Clay Bobby
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Don't know. I tried your quoted code on Ubuntu/JDK6u14 and it worked first time.
 
Clay Bobby
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome.

So you put a decimal in it and it worked? This is driving me up the wall.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Clay Bobby
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic