Scanner isn't accepting doubles
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Tim Driven Development | Test until the fear goes away
Fran Correia
Greenhorn
Posts: 12
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So it has to be a problem with my install of DrJava right? If it works everywhere else. I'll try re-installing again.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.

Tim Driven Development | Test until the fear goes away
Fran Correia
Greenhorn
Posts: 12
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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!
Thank you both!
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Country: Canada
Language: English
Decimal separator: .
Enter a double
Fran Correia
Greenhorn
Posts: 12
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Add these to your imports
Tim Driven Development | Test until the fear goes away
Fran Correia
Greenhorn
Posts: 12
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
Country: Portugal
Language: português
Decimal separator: ,
Can I change it to English?
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Great! Thank you so much for your help!
| If you have a bad day in October, have a slice of banana cream pie. And this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |












