parseDouble in Java results to NumberFormatException
Asked Answered
M

3

12

I am trying to load info from a properties file and i have the following code:

anInt = Integer.parseInt(prop.getProperty("anInt"));
aDouble = Double.parseDouble(prop.getProperty("aDouble"));

and while the first line works just fine, the second one where i am trying to load a double variable throws a NumberFormatException. The specific exception message is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "78,5"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at Assignment1.BaseStation.readPropertyFile(BaseStation.java:59)
at Assignment1.BaseStation.main(BaseStation.java:83)
Meagre answered 27/11, 2011 at 13:42 Comment(5)
What's the value of prop.getProperty("aDouble")?Shopping
it has to be a . not a ,.Shopping
And please next time, also give the message of the exception so that we can more easily see what the problem may be.Collude
i changed the , to a . and it still throws the same exceptionMeagre
prop.getProperty("anInt") and stack trace has nothing to do with the errorKowtow
M
21

You have to use a period as a delimiter, not comma if you want to parse using Double.parseDouble(). It says in documentation for the Double class that

FloatingPointLiteral ... [is] as defined in the lexical structure sections of the of the Java Language Specification.

From Java Language Specification:

FloatingPointLiteral:

  • Digits . Digits opt ExponentPart opt FloatTypeSuffix opt
  • . Digits ExponentPart opt FloatTypeSuffix opt
  • Digits ExponentPart FloatTypeSuffix opt
  • Digits ExponentPart opt FloatTypeSuffix

If you want to take locale into consideration, you can use java.text.NumberFormat:

NumberFormat nf = NumberFormat.getInstance();
double number = nf.parse(myString).doubleValue();
Monomial answered 27/11, 2011 at 13:47 Comment(7)
No! this is localization dependent.Kemberlykemble
Double.parseDouble() uses definitions as in Java language. And floating point literals in Java must use periods, not commas.Monomial
Hovercraft is right - values that use a comma as separator are parsed correctly if you take care about the localisation setting.Mccandless
@Mccandless Double.parseDouble() doesn't take locale into consideration, you have to use other method to do that. I have already cited documentation, and you may look through the source code if you still don't believe it.Monomial
@malcolm: i changed the commas to periods but the problem appears to be that when i am calling prop.getProperty to a line that contains a double value (e.g. 42.5) it returns a null pointer and not a string "42.5". Any ideas?Meagre
If getProperty() returns null, this can only mean that it can't find a property with this name. Check if you typed the property name right.Monomial
completely forgot it, sorry ;)Meagre
A
4

If you want to use "," as some EU countries using. You have to carefully take care of your localization.

Look at java api http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html

at valueOf, it said

To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.

For example, this code will solve your problem,

NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
double number = nf.parse(myString).doubleValue();

One important thing, you must not use something like

Locale.setDefault(something);

Because it can affect the whole JVM. In other words, that means it can affect the other codes which are using localization. Moreover it can affect the other apps which are in the same JVM if you are using Containner such as Servlet Container (such as shared Tomcat hosting).

And most of the time, something like Locale.setDefault() can be used in your local computer but you cannot deploy it on the other servers (shared tomcat hosting) because their JRE may be set permission to not allow to do such method. I'am pretty sure that most of good hosting providers did this. If you can deploy such this code on any shared Tomcats in any hosting providers, I strongly recommend you to change to another hosting company.

Anglicize answered 27/11, 2011 at 13:50 Comment(0)
M
3

"." is usually used as comma separator. If you'd like to stick with "," you'll have to change the localisation settings:

 Locale.setDefault(Locale.GERMAN);   //German will do the trick, better to use your country code

or make use of the 'NumberFormat' class. It's explained really well int this stackover flow thread

Mccandless answered 27/11, 2011 at 13:49 Comment(3)
A warning, if you change Locale.setDefault() it can affect the whole JVM. Then you have to be aware your app (your code which is relating to localization) and the other apps as well if you are using container like servlet container.Anglicize
That's true, better to use 'NumberFormat'.Mccandless
-1: Locale.setDefault() won't affect Double.parseDouble(), as it doesn't take locale into consideration. I've cited documentation in my answer, it is obvious from the OpenJDK source code, and also I've just checked it on my machine.Monomial

© 2022 - 2024 — McMap. All rights reserved.