When I attempt to convert a number from hexadecimal to decimal i.e., I enter 2 as the first input, the output comes,
'Enter a hexadecimal number
converted to decimal is 0'
It never waits for me to enter the string. I have commented where it doesn't accept the string. Can someone figure out what's wrong?
Thanks!
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
DecimalToHexadecimal ob=new DecimalToHexadecimal();
int op;
do
{
System.out.println("To convert decimal to hexadecimal enter 1 and to convert hexadecimal to decimal enter 2");
op=sc.nextInt();
if (op!=1 && op!=2)
{
System.out.println("Error!");
}
}while (op!=1 && op!=2);
if (op==1)
{
int n;
do
{
System.out.println("Enter a number");
n=sc.nextInt();
if (n<0)
{
System.out.println("Error!");
}
}while (n<0);
String hexa=ob.decimaltohexa(n);
System.out.println(n+" converted to Hexadecimal is "+hexa);
}
else
{
int d;
String hexa="";
do
{
System.out.println("Enter a hexadecimal number");
hexa=sc.nextLine(); //This is where it doesn't take the input
d=ob.hexatodeci(hexa);
if (d!=-1)
{
System.out.println(hexa+" converted to decimal is "+d);
}
if (d==-1)
{
System.out.println("Error!");
}
}while (d==-1);
}
}