Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

need help in converting decimal, binary, octal, and hexadecimal numbers

807605Aug 21 2007 — edited Aug 27 2007
I'm trying to make a program that can convert binary to decimal, decimal to binary, octal to hexadecimal, and hexadecimal to binary. It runs well, but I'm not getting the answers that I should be getting. I'll post the source code:

import java.io.*;
public class Compute {

public static void main (String[] args) {
int menu;
int num;
int rem;
String input;
String number;

BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));

try{
System.out.println("Main Menu: \nPlease type the number of your choice: \n 1 - Binary to Decimal \n 2 - Decimal to Binary \n 3 - Octal to Hexadecimal \n 4 - Hexadecimal to Binary \n 5 - Quit");
input = dataIn.readLine();
menu = Integer.parseInt(input);

switch(menu){
case 1:
System.out.println("Enter a Binary Number: ");
number=dataIn.readLine();
num=Integer.parseInt(number);
num=num + 2;
rem=num/2;
System.out.println(rem);
break;
case 2:
System.out.println("Enter a Decimal Number: ");
number=dataIn.readLine();
num=Integer.parseInt(number);
while (num/2 != 0){
rem=num%2;
num=num/2;
if(num==0)
break;
System.out.println(num + " base 10 = " + rem + " base 2");
}
break;

case 3:
System.out.println("Enter an Octal Number: ");
number=dataIn.readLine();
num=Integer.parseInt(number);
num=num + 3;
rem=num/2;
System.out.println(rem);
break;

case 4:
System.out.println("Enter a Hexadecimal Number: ");
number=dataIn.readLine();
num=Integer.parseInt(number);
num=num * 2;
rem=num-2;
System.out.println(rem);
break;

case 5:
System.out.println("Good-bye!");
System.exit(0);

}

}
catch (IOException x){
System.out.println("Error In Input");

}
catch (NumberFormatException e){
System.out.println("Error In Input. Please Try Again.");
}

}
}

Ignore cases 1, 3, and 4; I haven't figured out what to do to convert one to the other.

Anyway, the only case that's really working is case 2. However, when I run it, I get this:

Enter a Decimal number:
10
5 base 10 = 0 base 2
2 base 10 = 1 base 2
1 base 10 = 0 base 2

My main problems:
1) The answer is wrong; it should be 110 base 2 instead of 010 base 2.
2) The output should be 10 base 10 = 110 base 2, not three separate lines per computation.
3) The program exits after I execute this part. I need it to return to the main menu.
4) Say I put 'A' in the "Main Menu" or "Enter A Decimal Number" part. It's an exception, but the program exits immediately. Is it possible for it to return to the main menu?

Help would be greatly appreciated. :) Also, I would appreciate it if someone can tell me what to do for the other cases.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2007
Added on Aug 21 2007
9 comments
1,579 views