I kind of put aside this task some time ago and moved on. But today I decided to catch up with some unresolved tasks and got back to this one. The code seems to work fine in general, but there is still something wrong with the toBinary(String value) method, even though it produces and outputs the expected result.
The validator says it needs to be implemented as outlined in the task conditions. The conditions are these:
1) the inut parameter mustn't be empty:
2) if the input parameter contains any character other than digits from 0 to 9 or lowercase Latin letters from a to f, then the method returns an empty string.
I checked for those two things and made the method return "" in both cases. I also wrote two additional methods (decimalToBinary(int value) and letterToBinary(char value)) to help use the method in question (the toBinary(String value) one). Now it returns a string representation of the binary equivalent to the hexadecimal number given as an input parameter, but something is still wrong, according to the validator. Can you help me out, please?
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
if (binaryNumber == null || binaryNumber.isEmpty())
return "";
else {
for (int i = 0; i < binaryNumber.length(); i++) {
if (binaryNumber.charAt(i) != '0' && binaryNumber.charAt(i) != '1')
return "";
}
}
if (binaryNumber.length() % 4 != 0) { //We check if the length of the input string is a multiple of four
int extraZeroes = 4 - binaryNumber.length() % 4;
for (int i = 0; i < extraZeroes; i++) {
binaryNumber = "0" + binaryNumber; // we add extra zeroes at the beginning 0f the input string if necessary
}
}
//I'm not sure if I need to return the altered binaryNumber here
String hexNumber = "";
for (int i = 0; i < binaryNumber.length(); i += 4) { //we create a loop to go through the altered input string
String fourBits = binaryNumber.substring(i, (i + 4)); //we separate a substring of 4 bits
String fourBitsCoded = binaryToDecimal(fourBits); //string to string
fourBitsCoded = decimalToAF(fourBitsCoded); //string to string
hexNumber += fourBitsCoded;
//we concatenate the coded version of each four bits of the binary number to the hexNumber, which was empty in the beginning
}
return hexNumber;
}
public static String binaryToDecimal(String binary) {
if (binary == null || binary.isEmpty()) return "";
else {
int decimalNumber = 0;
for (int i = 0; i < binary.length(); i++) {
int index = binary.length() - 1 - i;
int value = Character.getNumericValue(binary.charAt(index));
decimalNumber += (int) (value * Math.pow(2, i));
}
return String.valueOf(decimalNumber);
}
}
public static String decimalToAF(String stringDecimal) {
if (stringDecimal == null || stringDecimal.isEmpty()) return "";
int tempNumber = Integer.valueOf(stringDecimal);
String result = "";//we need an integer here to compare it to 9
//if the decimal is less than or equal to 9, we return it as a string value
if (tempNumber <=9) result = stringDecimal;
//if the decimal is greater than 9 and less than 16, we convert it to a letter from a to f
else if (tempNumber > 9 && tempNumber < 16) {
String toSymbol;
switch (tempNumber) {
case 10:
toSymbol = "a";
break;
case 11:
toSymbol = "b";
break;
case 12:
toSymbol = "c";
break;
case 13:
toSymbol = "d";
break;
case 14:
toSymbol = "e";
break;
default:
toSymbol = "f";
}
result = toSymbol;
}
return result;
}
/*If the input parameter of the toBinary(String) method contains any character other than digits
from 0 to 9 or lowercase Latin letters from a to f,
then the method returns an empty string.*/
public static String toBinary(String hexNumber) {
// write your code here
StringBuilder binaryNumber = new StringBuilder();
if (hexNumber == null || hexNumber.isEmpty())
return "";
else if (!hexNumber.matches("[0-9a-f]+")) return "";
else if (hexNumber.matches("[0-9a-f]+")) {
for (int i = 0; i < hexNumber.length(); i++) {
char tempChar = hexNumber.charAt(i);
String tempString = "";
if (Character.isDigit(tempChar)) {
int tempInt = Character.getNumericValue(tempChar);
tempString = decimalToBinary(tempInt); //this method changes an int value into its binary euivalent's string representation
}
else if (Character.isLetter(tempChar)/* && Character.isLowerCase(tempChar)*/) {
tempString = letterToBinary(tempChar); //this method changes a char into its binary euivalent's string representation
}
binaryNumber.append(tempString);
}
}
return binaryNumber.toString();
}
public static String decimalToBinary(int decimalNumber) {
String result = "";
if (decimalNumber < 0) return "";
else if (decimalNumber == 0) {
result = "0000";
} else if (decimalNumber >= 0) {
while (decimalNumber != 0) {
result = decimalNumber % 2 + result;
decimalNumber = decimalNumber / 2;
}
}
return result;
}
public static String letterToBinary(char tempChar){
String result = "";
switch (tempChar) {
case 'a':
result = "1010";
break;
case 'b':
result = "1011";
break;
case 'c':
result = "1100";
break;
case 'd':
result = "1101";
break;
case 'e':
result = "1110";
break;
case 'f':
result = "1111";
break;
default:
result = "";
}
return result;
}
}