• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double Variable Problem  RSS feed

 
John Brendan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am testing to get a double value from keyboard input as below?


I type 5.95 in Console View of Eclipse but as a result I get an error on line 9.

What isvthe problem here?

Cheers
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the exact error message and stack trace so we can see what is happening.

As for the code posted it works on my machine.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is by any chance a comma character your default decimal separator?
Try entering 5,95 instead of 5.95.
Or add myScanner.useLocale(Locale.US);

Or even better. Don't use myScanner.nextDouble() for input.
Use myScanner.next() and then parse the resulting String according to your needs.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is by any chance a comma character your default decimal separator?

Good thinking.

Or even better. Don't use myScanner.nextDouble() for input.

I don't agree with this statement. Using nextDouble is generally the best way to get doubles entered, you just need to make sure you are using the correct locale for the user.

Use myScanner.nex() and then parse the resulting String according to your needs.

That should be: myScanner.next()
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:That should be: myScanner.next()

I know, I corrected my post 3 minutes 39 seconds before you posted yours :P
 
John Brendan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Please post the exact error message and stack trace so we can see what is happening.

As for the code posted it works on my machine.


Hi Tony,

I got this:



Cheers
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I don't agree with this statement. Using nextDouble is generally the best way to get doubles entered, you just need to make sure you are using the correct locale for the user.


Depends on your needs. Everything an user passes via keyboard is a String. Why not get every input value as String for further processing?
 
John Brendan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Is by any chance a comma character your default decimal separator?
Try entering 5,95 instead of 5.95.
Or add myScanner.useLocale(Locale.US);

Or even better. Don't use myScanner.nextDouble() for input.
Use myScanner.next() and then parse the resulting String according to your needs.


Hi Pawel,

Thank you very much for your help.

Yes, 5,95 helped. This seems a localization issue. How can we make sure the typed value gets converted into double by next()? Could you provide the code listing?

Cheers

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to follow my advice and use next() you would need to use Double.parseDouble to get the value.
IMO it is better approach as Double.parseDouble ignores the Locale and always uses a dot.
If you want do use nextDouble() you need to make sure that you pass a locale that uses a dot as a decimal separator to your Scanner.

John Brendan wrote:Could you provide the code listing?

Nope, we are not a code mill ;)
I already provided you what method to use. You need to write your code yourself.
 
John Brendan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:If you want to follow my advice and use next() you would need to use Double.parseDouble to get the value.
IMO it is better approach as Double.parseDouble ignores the Locale and always uses a dot.
If you want do use nextDouble() you need to make sure that you pass a locale that uses a dot as a decimal separator to your Scanner.

John Brendan wrote:Could you provide the code listing?

Nope, we are not a code mill ;)
I already provided you what method to use. You need to write your code yourself.


Hi Pawel,

I edited code:




But it throws error, if 5,95 is used. Is not there a default solution for Java that converts input to Double whether it is typed by comma or dot?

Cheers
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Brendan wrote:Is not there a default solution for Java that converts input to Double whether it is typed by comma or dot?

Nope, there is no such thing I am aware of.
But you can replace all ocurrences of commas into dots.
Use String.replace for that.
That's a plausible workaround.
 
John Brendan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
John Brendan wrote:Is not there a default solution for Java that converts input to Double whether it is typed by comma or dot?

Nope, there is no such thing I am aware of.
But you can replace all ocurrences of commas into dots.
Use String.replace for that.
That's a plausible workaround.

OK, I see.

Thank you very much for your all help.

Cheers
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point in favor of reading all user input as Strings (Scanner.next())

When there is such requirement (like accepting commas and dots), separating reading user input from handling it is a great idea.
Even if there is no such requirement it might appear in future. So you don't need a big refactoring (changing nextDouble to next everywhere). Just change the way Strings are handled.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!