# binary to decimal

Page 1 of 1

## 3 Replies - 4365 Views - Last Post: 01 November 2008 - 04:34 AMRate 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=69827&amp;s=759e433999d2dacecd4622e14fee8eb8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 side

Reputation: 0
• Posts: 11
• Joined: 28-October 08

# binary to decimal

Posted 31 October 2008 - 03:02 AM

hello~

i wanna user to input a binary int,
and print its decimal equivalent.
here r my code:
import java.util.Scanner;
import java.text.DecimalFormat;

public class NumberFormat
{
public static void main(String args[])
{
Scanner side=new Scanner(System.in);
int i=side.nextInt();

System.out.println("Binary: " + i + " = " + Integer.toDecimal);

}
}

it shows cannot find the symbol variable toDecimal

pls help me modify it!! thx

Is This A Good Question/Topic? 0

## Replies To: binary to decimal

### #2 priya_lorha

Reputation: 6
• Posts: 82
• Joined: 02-August 08

## Re: binary to decimal

Posted 31 October 2008 - 03:19 AM

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

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: binary to decimal

Posted 31 October 2008 - 06:57 PM

Better read is as a String not as a int nextInt()
you would be limited to:
1111111111111111 or around that

### #4 side

Reputation: 0
• Posts: 11
• Joined: 28-October 08

## Re: binary to decimal

Posted 01 November 2008 - 04:34 AM

priya_lorha, on 31 Oct, 2008 - 03:19 AM, said:

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

thx for the code~

i make ur code shorter,

but it cant work....

could u take a look for it? thx a lot!!
import java.util.Scanner;
import java.text.DecimalFormat;

public class NumberFormat
{
public static void main(String args[])
{
Scanner side=new Scanner(System.in);

System.out.println("Enter a Binary Number which is to be converted");

while(n>0)
{
r=n%10;
s=s+(r*p);
p=p*2;
n=n/10;
}
System.out.print("converted to Decimal Number="+s);
}
}

pbl, on 31 Oct, 2008 - 06:57 PM, said:

Better read is as a String not as a int nextInt()
you would be limited to:
1111111111111111 or around that

thx for ur reminder!!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }