-2

I have to compare two Strings by parsing them as Double.
The Strings are: "2.1.10" and "2.1.9". Here, I need to know whether the first one is greater than second one or not?
But, while parsing these using
Double.parseDouble("2.1.10") or Double.parseDouble("2.1.9") it is giving the NullPointerException saying "Multiple Points".

Saikiran Gosikonda
  • 638
  • 1
  • 7
  • 26
  • 2
    What does "2.1.10" mean? What did you expect?? – Tunaki Jan 28 '16 at 10:55
  • 1
    Possible duplicate: http://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java – assylias Jan 28 '16 at 10:57
  • @Tunaki, '2.1.10' and '2.1.9' are the identification numbers in my case. So I need to compare using these numbers – Saikiran Gosikonda Jan 28 '16 at 10:57
  • 1
    Well they're not *numbers*. What `double` value would you expect "2.1.10" to represent? It's simply not a number. I doubt that you're getting a `NullPointerException`, too - assuming it's actually a `NumberFormatException`, please edit the question accordingly. – Jon Skeet Jan 28 '16 at 11:00

5 Answers5

4

You don't compare doubles, you compare version numbers, and this look but are not numbers, you must compare each position.

Something like:

String[] v1 = "2.1.9".split("\\.");
String[] v2 = "2.1.10".split("\\.");

if (v1.length != v2.length)
    return;

for (int pos = 0; pos < v1.length; pos++) {
    // compare v1[pos] with v2[pos] as necessary
    if (Integer.parseInt(v1[pos]) > Integer.parseInt(v2[pos])) {
        System.err.println("v1 is greater");
    } else if (Integer.parseInt(v1[pos]) < Integer.parseInt(v2[pos])) {
        System.err.println("v2 is greater");
    }
}

RESULT:

v2 is greater
Jordi Castilla
  • 24,953
  • 6
  • 58
  • 97
  • It needs a little edit if you want to use this code. It works when the difference is only with the final number of the versions. It does not work comparing something v1 = "1.0.4" and v2 = 1.1.0". – Dracarys Apr 09 '18 at 10:32
2

The reason is because you have two decimal places in your number, which isn't allowed in maths. Remove one of the decimals from your string and it will work.

If you are using version numbers, break it down into three numbers and work along, like you would if you were checking alphabetical order.

William Dunne
  • 800
  • 6
  • 20
1

2.1.10 can never be mathematical number hence it is giving NumberFormatException. and even if you pass blank to Double.parseDouble("") still throw the same NumberFormatException.

you rather store "2.1.10" in String only instead of into any number type and let me know if you have any other option.

Vineet kaushik
  • 331
  • 3
  • 4
0

It is not possible as your value has two '.' and java does not check two decimal points. Remove one and then try. It is recommended to break down the value into two like for example 2.1.10 || 2.1.9 first check if 2.1 is the same.then check if 10/9 is same or greater. Hope this helped

BlueSlimShady
  • 127
  • 11
0

Since Double represents a numeric value, it should be mathematically correct. In other words there can be only one decimal place in a number.

Therefore Double.parseDouble("123.12") will execute properly and Double.parseDouble("123.1.2") will throw the exception which you've already got.

  • so what is the solution ? i want to parse values to compare app version numbers like 1.1.5 and 1.1.6 which datatype should i use – Kaustubh Bhagwat Oct 11 '17 at 10:11
  • you can compare decimal plates one by one. For example, For 1.1.5 and 1.1.6, get those as strings and split by "." . Take those elements of those arrays one by one and convert to integer. Then compare first number. (in this case both are 1s). Then compare the 2nd numbers. Then the thrid. Proceed like this. – Minudika Malshan Gammanpila Oct 13 '17 at 09:01