//Convert Binary Number to Decimal App import java.util.Scanner; public class BinaryToDecimal { public static void main(String[] args) { int r; int s = 0; int p = 1; Scanner in = new Scanner(System.in); System.out.println("Binary to Decimal Convert App"); System.out.println("Enter a Binary Number:");//prompt for binary number to be converted int n= in.nextInt(); { int temp = n; while(n > 0) { r = n % 10; temp = n / 10; if(r!= 0 && r!= 1) { System.out.println("This is not a binary number!"); System.exit(0);//exits if the number is non binary } } System.out.print("Binary="+n); while(n>0) { r=n%10; s=s+(r*p); p=p*2; n=n/10; } System.out.print("Converted to Decimal Number="+s);//generates output } // End Method main }} // End of class
Binary to Decimal Conversion
Page 1 of 15 Replies  12949 Views  Last Post: 14 February 2010  08:03 PM
#1 Guest_Theresa Schultz*
Binary to Decimal Conversion
Posted 14 February 2010  02:55 PM
I have an assignment to write a java code to convert a binary number to a decimal number and print it. Here is what I have so far and I am stuck and could realy use some help. Please forgive me because I was really having a hard time wrapping my head around this for some reason. Please give me any help you can; it would be greatly appreciated. The file compiled without errors but it didn't print out anything; it did nothing after I entered the number.
Replies To: Binary to Decimal Conversion
#2
Re: Binary to Decimal Conversion
Posted 14 February 2010  03:06 PM
So you commented this:
And didn't commented this?
...
System.out.println("Enter a Binary Number:");//prompt for binary number to be converted
And didn't commented this?
int r; int s = 0; int p = 1;
...
#3 Guest_Theresa Schultz*
Re: Binary to Decimal Conversion
Posted 14 February 2010  03:58 PM
anonymouscodder, on 14 February 2010  02:06 PM, said:
So you commented this:
And didn't commented this?
...
System.out.println("Enter a Binary Number:");//prompt for binary number to be converted
And didn't commented this?
int r; int s = 0; int p = 1;
...
I know. I am lost. I tried to get some help from a previous post (http://www.dreamincode.net/forums/index.php?showtopic=153586&st=20) but I think it just confused me more. Maybe I should start over. I have to write a beginner program to convert a binary integer to a decimal integer and they want us to use division and remainder to get there. Can you provide any help? Of course my assignment is due at midnight tonight and I didn't realize I would have so much trouble with this but for some reason I have just hit a brick wall with this and don't know where to start...
#4
Re: Binary to Decimal Conversion
Posted 14 February 2010  04:14 PM
The reson is that after checking if n is a binary number, the value of n is 0.
then the while loop to convert n to decimal will NEVER execure. because n is never bigger than 0.
you can have two options.
1. you can set a temp variable to hold the value of n, and perform the check on this variable.
2. do both checking and converting in the same loop:
option 2:  converting and checking in the same loop
then the while loop to convert n to decimal will NEVER execure. because n is never bigger than 0.
you can have two options.
1. you can set a temp variable to hold the value of n, and perform the check on this variable.
2. do both checking and converting in the same loop:
option 2:  converting and checking in the same loop
import java.util.Scanner; public class TestClass{ public static void main(String[] args) { //binary value int bin = 0; //decimal value int decimal = 0; //Scanner object Scanner s = new Scanner(System.in); //the pow of 2 int p = 1; //getting binary number System.out.println("Enter Binary Number"); bin = s.nextInt(); //the loop  checking and converting while(bin > 0){ int decimalDig = bin % 10; if(decimalDig != 0 && decimalDig != 1){ System.out.println("Number is not Binary"); System.exit(0); } //converting else { decimal += decimalDig * p; p *= 2; bin /= 10; } } //printing decimal value System.out.println("Decimal Value is: " + decimal); } }
#5 Guest_Theresa Schultz*
Re: Binary to Decimal Conversion
Posted 14 February 2010  04:33 PM
You are AWESOME! That made SO much more sense to me than what I was trying to do. Thanks SO MUCH!
#6
Re: Binary to Decimal Conversion
Posted 14 February 2010  08:03 PM
you should really think about naming your variables something more straight forward.
Page 1 of 1
