-2

I'm trying to parse a string a in java and this is the error that I get.

<gx:coord>10,8638931 44,6106521 0</gx:coord>

 

Exception in thread "main" java.lang.NumberFormatException: For input string: "10,8638931"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at parser.history.Main.main(Main.java:28)

Can anyone tell me why?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
AlketCecaj
  • 117
  • 1
  • 11

2 Answers2

1

The Double.parseDouble method does not accept numbers with commas in them. The input string must conform to the syntax described in the javadocs for Double.valueOf(String).

If you want to parse numbers in other formats, you should look at the DecimalFormat class, and specifically its parse method. Depending on the circumstances, you could get a DecimalFormat instance from the Locale ... or create it specifically using a given pattern and symbols.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

java uses a dot instead of a comma to parse the floating number.

Call

String.replace(',','.')

before calling Double.parseDouble

Pierre
  • 34,472
  • 31
  • 113
  • 192