dzenesiz
dzenesiz

Reputation: 1542

Java: formatting numbers error?

I am encountering a problem regarding a class which converts number formats with String input and output:

public class Conversion{

static int result;

public static String fromHexToDec (String clientInput) {
    result= Integer.parseInt(clientInput, 10);
    return Integer.toString(result);
}

public static String fromDecToHex (String clientInput) {
    result= Integer.parseInt(clientInput, 16);
    return Integer.toString(result);
}

public static String fromOctTo4 (String clientInput) {
    result= Integer.parseInt(clientInput, 4);
    return Integer.toString(result);
}

public static String from4ToOct(String clientInput) {
    result= Integer.parseInt(clientInput, 8);
    return Integer.toString(result);
}

public static String formBinToDec(String clientInput) {
    result= Integer.parseInt(clientInput, 10);
    return Integer.toString(result);
}

public static String fromDecToBin(String clientInput) {
    result= Integer.parseInt(clientInput, 2);
    return Integer.toString(result);
}

public static String from5To7(String clientInput) {
    result= Integer.parseInt(clientInput, 7);
    return Integer.toString(result);
}

public static String from7To5(String clientInput) {
    result= Integer.parseInt(clientInput, 5);
    return Integer.toString(result);
}

}

I am receiving this error trying to run fromHexToDec in the the main method:

Exception in thread "main" java.lang.NumberFormatException: For input string: "C"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Konverzija.fromHexToDec(Conversion.java:9)
at Test.main(Test.java:5)

Can somebody help, please?

Upvotes: 2

Views: 384

Answers (2)

ulix
ulix

Reputation: 999

Integer.parseInt(String s, int radix) should be used to parse, or "transform" a string value into an integer value. The radix in this case is the radix you expect the string representation of this number to have. It means if you are expecting s to be an hexadecimal representation of a number you should use 16 as the radix and so on. The Integer.toString(int value, int radix) method is just the opposite: used to transform the integer value into it's string representation in the given radix. In this case you would specify 16 for an hexadecimal representation. All radix values you specified for parsing are wrong and you missed radixes for the calls to toString(). You can only omit the radix parameter of these methods when you want to use the default radix of 10 (decimal).
Your code should look like this:

public class Conversion{

    static int result;

    public static String fromHexToDec (String clientInput) {
        result= Integer.parseInt(clientInput, 16);
        return Integer.toString(result);
    }

    public static String fromDecToHex (String clientInput) {
        result= Integer.parseInt(clientInput);
        return Integer.toString(result, 16);
    }

    public static String fromOctTo4 (String clientInput) {
        result= Integer.parseInt(clientInput, 8);
        return Integer.toString(result, 4);
    }

    public static String from4ToOct(String clientInput) {
        result= Integer.parseInt(clientInput, 4);
        return Integer.toString(result, 8);
    }

    public static String formBinToDec(String clientInput) {
        result= Integer.parseInt(clientInput, 2);
        return Integer.toString(result);
    }

    public static String fromDecToBin(String clientInput) {
        result= Integer.parseInt(clientInput);
        return Integer.toString(result, 2);
    }

    public static String from5To7(String clientInput) {
        result= Integer.parseInt(clientInput, 5);
        return Integer.toString(result, 7);
    }

    public static String from7To5(String clientInput) {
        result= Integer.parseInt(clientInput, 7);
        return Integer.toString(result, 5);
    }
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201409

Because

public static String fromHexToDec (String clientInput) {
    result= Integer.parseInt(clientInput, 10);
    return Integer.toString(result);
}

is passing radix 10 to parseInt() (and that's not valid in decimal). Use 16 like

public static String fromHexToDec (String clientInput) {
    result= Integer.parseInt(clientInput, 16);
    return Integer.toString(result);
}

And fromDecToHex should probably be

public static String fromDecToHex(String clientInput) {
    int result = Integer.parseInt(clientInput, 10);
    return Integer.toString(result, 16);
}

or

public static String fromDecToHex(String clientInput) {
    int result = Integer.parseInt(clientInput);
    return String.format("%02X", result);
}

the second has the potential advantage of being zero filled.

Upvotes: 5

Related Questions