• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner isn't accepting doubles  RSS feed

 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new to java and I'm taking a computer science class just for fun.
When completing an assignment that required the use of Scanner I came across a problem that I can't find a solution to.
I had to use scanner to receive input from the user for a formula and everything compiled and worked correctly except when I entered a double into the Scanner, it only accepts ints and gives me this error every time I enter a double:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TestA1Q2.main(TestA1Q2.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I know that you usually get that error when the Scanner expects something else that is different from what you type and I have looked over my code time and time again and cannot find anything. I created a test program to see if it was just that problem and not something else in the code and I still got the same error but when I tried compiling and running it here browxy.com everything worked and I got the right answer for the formula. Here's the test I used:

At this point I'm thinking it might be a problem with my install of DrJava and I've tried re-installing but nothing changed. Can anyone help?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be a i18n issue - 123.45 vs. 123,45? What exactly did you enter which caused the exception to be thrown?
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried all kinds of doubles, the one I was using for the formula was 1.2 but any other double gave the same error.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works alright for me when entering doubles. You'll get that error message if you enter something that isn't a double, say like an alpha character.
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it has to be a problem with my install of DrJava right? If it works everywhere else. I'll try re-installing again.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - I edited my post at the same time you were responding.

If I change my locale from English/Canada to French/Canada, I get an exception when I enter 1.2, but it does work with 1,2 .

It might be a long shot, but check to see if there could be anything related to locale.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ron, would you mind please making a new post when replying to a thread rather than updating your existing post. Think of this as a conversation. Going back and changing something you've already said can make the conversation confusing at best, and at worst can make the posts after yours look like nonsense if they're referring to something you said before you edited it.

Thanks

Edit: Ron, Your previous reply came in at the same time as I posted this one. I take it back. You know the score.
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't even thought of that, yes it works with 1,2. I think my professor always uses . instead of , though. I think in other cases it always accepts . when using a double. Using the scanner is the first issue I've had with this.

Thank you both!
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To troubleshoot further, you could print-out the locale specifics for the different environments that you have tested-in. For me:

Country: Canada
Language: English
Decimal separator: .
Enter a double


 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I pasted that in it gave me 4 errors:

4 errors found:
File: C:\COMP\Java Labs\Assignments\TestA1Q2.java [line: 5]
Error: cannot find symbol
symbol: class Locale
location: class TestA1Q2
File: C:\COMP\Java Labs\Assignments\TestA1Q2.java [line: 5]
Error: cannot find symbol
symbol: variable Locale
location: class TestA1Q2
File: C:\COMP\Java Labs\Assignments\TestA1Q2.java [line: 10]
Error: cannot find symbol
symbol: class DecimalFormat
location: class TestA1Q2
File: C:\COMP\Java Labs\Assignments\TestA1Q2.java [line: 10]
Error: cannot find symbol
symbol: variable DecimalFormat
location: class TestA1Q2


Is there something I need to change for it to work? I'm sorry about this, I don't know much about java.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add these to your imports
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added, I didn't know it used my windows language I should have assumed that. My windows is Portuguese so it gives me:
Country: Portugal
Language: português
Decimal separator: ,

Can I change it to English?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If required, you can change the locale for your application using Locale.setDefault(..). For example:


Country: Estados Unidos
Language: español
Decimal separator: .
Enter a double


Whoops - should be Locale("en", "US") for US/English
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thank you so much for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!