# Binary to Decimal

• (2 Pages)
• 1
• 2

## 23 Replies - 3641 Views - Last Post: 05 February 2010 - 02:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=153586&amp;s=ccc40a73fe40907b95c10ca53724522d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

# Binary to Decimal

Posted 02 February 2010 - 08:09 PM

Hi I am trying to write an app that inputs an integer containing only 0s and 1s (a binary integer) and prints its decimal equivalent. I am aware that there are some very short ways to do this but as a beginner I am writing this to show the process. I am trying to use the remainder and division operators to complete this. Here is what I have, is there a different route I can take? Any help will be greatly appreciated! thanks :)

public class BinarytoDecimal {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
while(n > 0)
{
r = n % 10;
n = 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

Is This A Good Question/Topic? 0

## Replies To: Binary to Decimal

### #2 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Binary to Decimal

Posted 02 February 2010 - 08:30 PM

I haven't looked at the logic of this program yet, but I did see something that made me flinch.

Nowhere, is "in" defined. It appears that you are trying to use a BufferedReader, but I suggest Scanner to read from command line..
Scanner in = new Scanner(System.in); // Makes a command-line scanner

// NOW, you can do this:
int n=Integer.parseInt(in.nextLine());

// Or better:
int n= in.nextInt();

Here's the API for Scanner: http://java.sun.com/...il/Scanner.html

This post has been edited by Dogstopper: 02 February 2010 - 08:31 PM

### #3 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Binary to Decimal

Posted 02 February 2010 - 08:37 PM

Also, your entire block of code is out side of your main method. And out side of your class for that matter.

### #4 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Binary to Decimal

Posted 02 February 2010 - 08:39 PM

erik.price, on 02 February 2010 - 09:37 PM, said:

Also, your entire block of code is out side of your main method. And out side of your class for that matter.

*facepalm* I think it's time for bed. I missed that one....badly...

### #5 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Binary to Decimal

Posted 02 February 2010 - 08:44 PM

No worries, I miss blatantly obvious things constantly :)

Anyway, this isn't really an error, but
System.out.println("This is not a binary number.!!!");
System.exit(0);//exits if the number is non binary

It asks you to try again, and then the program just ends.

You may want to either prompt the user for more input, or simply quit without taunting them like that

This post has been edited by erik.price: 02 February 2010 - 08:45 PM

### #6 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

## Re: Binary to Decimal

Posted 02 February 2010 - 09:04 PM

Yea I couldn't figure where to properly place by brackets srry

### #7 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

## Re: Binary to Decimal

Posted 02 February 2010 - 09:24 PM

This is what I have now?

public class BinarytoDecimal {

/**
* @param args
*/
public Main() {

}

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
int n= in.nextInt();

while(n > 0)
{
r = n % 10;
n = 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

### #8 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Binary to Decimal

Posted 03 February 2010 - 01:15 PM

You definitely want to work on your indentions...they are quite confusing at the moment.

public class BinarytoDecimal {

/**
* This "constructor shouldn't be here anymore.
* The class is called BinaryToDecimal, not Main...
* Also, no need for a default constructor that does nothing.
*/
public Main() {

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
int n= in.nextInt();

while(n > 0)
{
r = n % 10;
n = 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

// Need ending brackets!
} // End of main()
} // End of class.

That should allow it to at least compile.

### #9 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Binary to Decimal

Posted 03 February 2010 - 01:49 PM

Why do you have this?
public Main() {

}

Your class is called BinaryToDecimal, so I don't understand why you have a constructor for a class called Main. Remove that.

### #10 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

## Re: Binary to Decimal

Posted 03 February 2010 - 03:29 PM

Thanks guys, I tried to fix these errors now there is something wrong with my variables.

public class BinarytoDecimal {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
int n= in.nextInt();

while(n > 0)
{
r = n % 10;
n = 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

{}// Need ending brackets!
} // End of main()
} // End of class

### #11 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Binary to Decimal

Posted 03 February 2010 - 03:30 PM

You never declare r, s, and p

### #12 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

## Re: Binary to Decimal

Posted 03 February 2010 - 04:05 PM

Oh okay, what is wrong with my scanner now
public class BinarytoDecimal {

public static void main(String[] args) {

int r;
int s;
int p;

Scanner in = new Scanner(System.in);
System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
int n= in.nextInt();

while(n > 0)
{
r = n % 10;
n = 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

{}// Need ending brackets!
} // End of main()
} // End of class

### #13 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Binary to Decimal

Posted 03 February 2010 - 04:19 PM

Are you getting any errors in your code?

### #14 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Binary to Decimal

Posted 03 February 2010 - 04:22 PM

Did you remember to import the Scanner class?
import java.util.Scanner;

public class BinarytoDecimal {
//.....

### #15 ThisGirly

Reputation: 0
• Posts: 28
• Joined: 23-January 10

## Re: Binary to Decimal

Posted 03 February 2010 - 04:32 PM

Ok I don't have any errors that show now But when I run my program and enter binary inters the decimal equivalent always comes out to 0. What should I initialize my variables to? Here's what I have
import java.util.Scanner;

public class BinarytoDecimal {

public static void main(String[] args) {

int r;
int s = 0;
int p = 0;

Scanner in = new Scanner(System.in);
System.out.println("Binary to Decimal");
System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
int n= in.nextInt();

while(n > 0)
{
r = n % 10;
n = 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

{}// Need ending brackets!
} // End of main()
} // End of class

thanks erik
thanks japanir

binary integers* srry