From c2cfc021c793d82ac3b24a9611ffa85897c486a4 Mon Sep 17 00:00:00 2001 From: Anand Kumar Jha <66514444+anandjha-git@users.noreply.github.com> Date: Mon, 10 Jul 2023 00:13:29 +0530 Subject: [PATCH] Update CompareBinaryToHex.java We have only two bases, 2 and 16. No need to check the base for else. --- .../CompareBinaryToHex/CompareBinaryToHex.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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) {