RajSanpui
RajSanpui

Reputation: 12094

In Java how to parse a string to a float and compare the value?

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");

}

Upvotes: 2

Views: 1794

Answers (8)

anon
anon

Reputation:

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");
}

Upvotes: 4

Yasser Zamani
Yasser Zamani

Reputation: 2500

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;
}

Upvotes: 0

RunasSudo
RunasSudo

Reputation: 493

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.

Upvotes: 1

Grooveek
Grooveek

Reputation: 10094

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

Upvotes: 0

Raffaele
Raffaele

Reputation: 20885

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.

Upvotes: 1

LPD
LPD

Reputation: 2883

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..

Upvotes: 1

Duncan Jones
Duncan Jones

Reputation: 69369

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.

Upvotes: 0

gefei
gefei

Reputation: 19816

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.

Upvotes: 1

Related Questions