I'm stuck with the first and second requirement task....for binarynumber input ,I tried to use null as input....the result is null pointer exception....how to solve this task...some advice please
package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
public class Solution {
public static void main(String[] args) {
String binaryNumber =null;//"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
String a="";
int x;
int c=0 ;
char[] ch= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int b = binaryNumber.length()%4 ;
if(binaryNumber.isEmpty()) {return a ;}
else
for(int i=0;i<binaryNumber.length();i++) {
x=(int)Math.pow(2,binaryNumber.length()-1-i) ;
if(binaryNumber.charAt(i)=='1' )
{c=c+x;}
}
while(c>0) {
a=ch[c%16]+a;
c=c/16;
}
return a;
}
public static String toBinary(String hexNumber) {
//write your code here
String a="" ;
int b=0;
String ch= "0123456789abcdef" ;
if(hexNumber==null) {return a;}
else
for(int i=0;i<hexNumber.length();i++) {
b=b+ch.indexOf(hexNumber.charAt(i)) *(int) Math.pow(16,hexNumber.length()-1-i) ;
}
while(b>0) {
a=b%2+ a;
b=b/2;
}
return a;
}
}