0

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

            }

1 Answers1

0

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();
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • thank you very much, it is now working like I wanted it to. Your help is much appreciated. –  Nov 27 '16 at 16:01