Search code examples
javastring-parsing

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

}

Solution

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