When I run the verification, I keep failing the 2nd test as shown
![]()

package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
public class Solution {
public static void main(String[] args) {
String binaryNumber = "100111010000";
System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber));
String hexNumber = "9d0";
System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber));
}
public static String toHex(String binaryNumber) {
//write your code here
//declare variables
int leftIndex = 0, rightIndex = 4;
String hexString = "", emptyStr = "", hexItem = "";
String binPart;
String[] binaryArr = {"00000", "10001", "20010", "30011", "40100", "50101", "60110", "70111", "81000", "91001", "A1010", "B1011", "C1100", "D1101", "E1110", "F1111"};
boolean chklen;
boolean isFound = false;
int remainder = 0;
if (binaryNumber == null || binaryNumber.isEmpty()) {
return emptyStr;
} else {
//check if the binaryNumber is long enough (a multiple of 4)
if (chklen = binaryNumber.length() % 4 == 0) {
//loop through binaryNumber
for (int i = 0; i < binaryNumber.length()/4; i++) {
//get the next four digits of binaryNumber
binPart = binaryNumber.substring(leftIndex, rightIndex);
leftIndex += 4;
rightIndex += 4;
isFound = false;
//find a match for the binary number in the binary array
for (String binEle : binaryArr) {
if (binEle.endsWith(binPart)) {
//get the corresponding hex digit from the string element of the binary array
hexItem = binEle.substring(0, 1);
isFound = true;
break;
}
}
//if I loop through all the binary digits in the array and don't find a match, return an empty string
if (!isFound) {
return emptyStr;
}
//construct hexNumber String
hexString += hexItem;
}
//if the binaryNumber is not long enough, add digits then perform operations
} else {
remainder = binaryNumber.length() % 4;
switch (remainder) {
case 1: {
binaryNumber = "000" + binaryNumber;
isFound = false;
for (int i = 0; i < binaryNumber.length()/4; i++) {
//get the next four digits of binaryNumber
binPart = binaryNumber.substring(leftIndex, rightIndex);
leftIndex += 4;
rightIndex += 4;
//find a match for the binary number in the binary array
for (String binEle : binaryArr) {
if (binEle.endsWith(binPart)) {
//get the corresponding hex digit from the string element of the binary array
hexItem = binEle.substring(0, 1);
isFound = true;
break;
}
}
//if I loop through all the binary digits in the array and don't find a match, return an empty string
if (!isFound) {
return emptyStr;
}
//construct hexNumber String
hexString += hexItem;
}
}
case 2: {
binaryNumber = "00" + binaryNumber;
isFound = false;
for (int i = 0; i < binaryNumber.length()/4; i++) {
//get the next four digits of binaryNumber
binPart = binaryNumber.substring(leftIndex, rightIndex);
leftIndex += 4;
rightIndex += 4;
//find a match for the binary number in the binary array
for (String binEle : binaryArr) {
if (binEle.endsWith(binPart)) {
//get the corresponding hex digit from the string element of the binary array
hexItem = binEle.substring(0, 1);
isFound = true;
break;
}
}
//if I loop through all the binary digits in the array and don't find a match, return an empty string
if (!isFound) {
return emptyStr;
}
//construct hexNumber String
hexString += hexItem;
}
}
default: {
binaryNumber = "0" + binaryNumber;
isFound = false;
for (int i = 0; i < binaryNumber.length()/4; i++) {
//get the next four digits of binaryNumber
binPart = binaryNumber.substring(leftIndex, rightIndex);
leftIndex += 4;
rightIndex += 4;
//find a match for the binary number in the binary array
for (String binEle : binaryArr) {
if (binEle.endsWith(binPart)) {
//get the corresponding hex digit from the string element of the binary array
hexItem = binEle.substring(0, 1);
isFound = true;
break;
}
}
//if I loop through all the binary digits in the array and don't find a match, return an empty string
if (!isFound) {
return emptyStr;
}
//construct hexNumber String
hexString += hexItem;
}
}
}
}
return hexString;
}
}
public static String toBinary(String hexNumber) {
//write your code here
//declare variables
String emptyStr = "";
String binaryString = "";
String binaryItem = "";
String[] binaryRepr = {"00000", "10001", "20010", "30011", "40100", "50101", "60110", "70111", "81000", "91001", "A1010", "B1011", "C1100", "D1101", "E1110", "F1111"};
char hexChar;
boolean isFound = false;
//check if null or empty string has been provided, and return empty string
if (hexNumber == null || hexNumber.isEmpty()) {
return emptyStr;
} else {
//return the binary string representation
//loop through the hex string representation
for (int i = 0; i < hexNumber.length(); i++) {
hexChar = hexNumber.charAt(i);
//get the binary string from the location given by hexChar
//compare hexChar to the beginning of each element in binaryRepr
for (String binEle : binaryRepr) {
if (binEle.startsWith((hexChar + "").toUpperCase())) {
binaryItem = binEle.substring(1, 5);
isFound = true;
break;
}
}
//if nothing is found, return empty string, else
//concatenate binary String to binaryItem
if (isFound == false) {
return emptyStr;
} else {
binaryString += binaryItem;
}
}
return binaryString;
}
}
}