diff --git a/Java/Introduction/CompareBinaryToHex/CompareBinaryToHex.java b/Java/Introduction/CompareBinaryToHex/CompareBinaryToHex.java index 48ec7badd..8f7bfe27a 100644 --- a/Java/Introduction/CompareBinaryToHex/CompareBinaryToHex.java +++ b/Java/Introduction/CompareBinaryToHex/CompareBinaryToHex.java @@ -14,8 +14,8 @@ public static int digitToValue(char c) { } public static int convertFromBase(String number, int base) { - if (base < 2 || (base > 10 && base != 16)) return -1; - int value = 0; + if (base == 2 || base == 16) { + int value = 0; for (int i = number.length() - 1; i >= 0; i--) { int digit = digitToValue(number.charAt(i)); if (digit < 0 || digit >= base) { @@ -25,6 +25,9 @@ public static int convertFromBase(String number, int base) { value += digit * Math.pow(base, exp); } return value; + } + return -1; + } public static boolean compareBinToHex(String binary, String hex) {