Why I'm not able to pass the vaildation?
This is the comment from my mentor but I'm not able to see my mistake.
Be sure that the toDecimal(String) method returns 0 if it receives an empty string or null as input.
Some help, please?
package en.codegym.task.pro.task09.task0906;
/*
Binary converter
*/
public class Solution {
public static void main(String[] args) {
int decimalNumber = Integer.MAX_VALUE;
System.out.println("Decimal number " + decimalNumber + " is equal to binary number " + toBinary(decimalNumber));
String binaryNumber = "1111111111111111111111111111111";
System.out.println("Binary number " + binaryNumber + " is equal to decimal number " + toDecimal(binaryNumber));
}
public static String toBinary(int decimalNumber) {
//write your code here
if(decimalNumber < 0) return "";
String binaryRep = "";
while(decimalNumber !=0){
binaryRep = (decimalNumber%2) + binaryRep;
decimalNumber = decimalNumber/2;
}
return binaryRep;
}
public static int toDecimal(String binaryNumber) {
//write your code here
if(binaryNumber.isEmpty() || binaryNumber.equals(null)) return(int) 0;
long decimalNumber = 0;
for(int i = 0; i < binaryNumber.length(); i++){
char digit = binaryNumber.charAt(binaryNumber.length() - i - 1);
int digito = (digit=='0')?0:1;
decimalNumber = (long) (decimalNumber + Math.pow((digito*2),i));
}
return (int) decimalNumber;
}
}