2

I have a small code snippet as shown below, which as you can see has a hard-coded value for checking server version. Now my intention is, if the server version is 11.3.0 or higher, then the if should be entered, but i am not able to figure out a way, Integer.parseInt won't work i guess as i parses int not float.

String serverVersion = DatamodelVersion.getInstance().getVersion();
if(serverVersion.equalsIgnoreCase("11.3.0"))
{
    outstr = new FileOutputStream(confFile);
    prop.setProperty("NTFSDriver", "11.3.0/x86/tntfs.ko");
    prop.setProperty("NTFSDriver_x64", "11.3.0/x86_64/tntfs.ko");

    prop.store(outstr, "");

    update = true;
    System.out.println("Updated the tuxera conf file successfully");
    logger.logDebugAlways("Updated the tuxera conf file successfully");

}
Kjartan
  • 18,591
  • 15
  • 71
  • 96
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
  • 2
    Treat it like this: its not one number, there are three numbers. Does that make the problem easier? It should. – Gimby Dec 21 '12 at 10:24
  • 2
    `11.3.0` is not a float either. – dogbane Dec 21 '12 at 10:24
  • @Gimby: Okay, take it as 11.3 instead of 11.3, that makes the problem one step harder :( – RajSanpui Dec 21 '12 at 10:26
  • Version numbers can't be accurately represented in a float. (In fact very few things can be truly accurately represented in a float, but that's a different matter.) Version numbers like this should be treated as a list of integers over which a simple lexicographic ordering is defined. – biziclop Dec 21 '12 at 10:27
  • 3
    possible duplicate of [How do you compare two version Strings in Java?](http://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java) – dogbane Dec 21 '12 at 10:27

8 Answers8

4

Try this

String serverVersion = DatamodelVersion.getInstance().getVersion();
String[] version = serverVersion.split("\\.");
if (Integer.parseInt(version[0]) > 11 || (Integer.parseInt(version[0]) == 11 && Integer.parseInt(version[1]) >= 3))
{
    outstr = new FileOutputStream(confFile);
    prop.setProperty("NTFSDriver", "11.3.0/x86/tntfs.ko");
    prop.setProperty("NTFSDriver_x64", "11.3.0/x86_64/tntfs.ko");

    prop.store(outstr, "");

    update = true;
    System.out.println("Updated the tuxera conf file successfully");
    logger.logDebugAlways("Updated the tuxera conf file successfully");
}
1

there is not a built-in function in Java to transform 11.3.0 to float, because 11.3.0 is not a valid float number.

for strings containing a valid float number, you could use Float.valueOf in Java.

gefei
  • 18,922
  • 9
  • 50
  • 67
1

Split the version number by "."

Then compare one by one with your reference data.

String serverVersion = DatamodelVersion.getInstance().getVersion();
serverVersion.split('.')[0] // and so on..
LPD
  • 2,833
  • 2
  • 29
  • 48
1

A version number is neither an integer, nor a float. Your best bet is using a specialized class:

public class Version implements Comparable<Version> {

  public Version(int major, int minor, int revision) {
    // set fields
  }

  public int compareTo(Version other) {
    // compare major minor and revision
  }

  public boolean equals(Object other) {
    // code here
  }      

  // parse a version in X.Y.Z form
  static Version parse(String version) {
    return new Version(//...);
  }
}

Using this you may decide to later add support for versions like 1.3.4-ALPHA or -RC1 and the like.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
1

You could try splitting the number into 3 parts, like so:

String[] bits = serverVersion.split(".");

Then, use a for loop and Integer.parseInt to parse each section of the number, and compare each.

RunasSudo
  • 483
  • 2
  • 4
  • 13
0

You need to define your own method for checking version numbers, based on the rules these numbers must follow in your domain.

Based on the information you've provided, I would do a String split on . and compare each value in turn (as an integer) against 11, 3 and 0 respectively.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

You have a Version class here : http://nvn.svn.sourceforge.net

It should be used like this

Version v = Version.parse(DatamodelVersion.getInstance().getVersion());

and store numbers in the standard format MAJOR.MINOR.BUILD.REVISION

Grooveek
  • 10,046
  • 1
  • 27
  • 37
0

11.3.0 is not a float number. What about to use following code instead:

int Compare(String ver1, String ver2)
{
String[] ver1s = ver1.Split("\.");
String[] ver2s = ver2.Split("\.");
if(ver1s.length > ver2.length) return 1;
if(ver2s.length > ver1.length) return -1;
for(int i = 0;i < ver1s.length;i++)
{
   if(Integer.valueOf(ver1s[i]) > Integer.valueOf(ver2s[i])) return 1;
   if(Integer.valueOf(ver2s[i]) > Integer.valueOf(ver1s[i])) return -1;
}
return 0;
}
Yasser Zamani
  • 2,380
  • 21
  • 18