rootsquaredtimes89 rootsquaredtimes89 - 9 days ago 8
Java Question

Get Windows version with Java is not working

I have a code which is supposed to get the current OS version, so lets say the computer I use, is running Windows 10 then it would output 10. The code in theory should work, but there is a slight problem. The code itself does not give any errors, but whenever I run it I get the console error.


Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: For input string: "10.0"


I am not completely sure where my code is wrong, but there is definitely something which is not connecting.

Here is the code



private static String OSV = System.getProperty("os.version").toLowerCase();
static int OSVnum = Integer.parseInt(OSV);


static void windRoc() throws IOException {

if (OSVnum == 10.0) {
System.out.println("It is working!");
} else if (OSVnum == 7) {
stop1();
} else {
stop2();
}

}

Answer

You are currently trying to parse a floating point number as an integer, which doesn't work. Instead, try using Double.parseDouble:

static double OSVnum = Double.parseDouble(OSV);

Then make your checks as you were previously:

static void windRoc() throws IOException {
    if (OSVnum == 10.0) {
        System.out.println("It is working!");
    } else if (OSVnum == 7) {
        stop1();
    } else {
       stop2();
    }
}