3 Replies - 3866 Views - Last Post: 01 November 2008 - 04:34 AM Rate Topic: -----

#1 side  Icon User is offline

  • New D.I.C Head

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

binary to decimal

Post icon  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);
	System.out.print("please enter a binary number:");
	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  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • 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
int n=Integer.parseInt(in.readLine());
while(n > 0)
{
r = n % 10;
n = n / 10;
if(r!= 0 && r!= 1)
{
System.out.println("This is not a binary number.!!!");
System.out.println("Please try once again.!!!");
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
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • 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
Was This Post Helpful? 1
  • +
  • -

#4 side  Icon User is offline

  • New D.I.C Head

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

Re: binary to decimal

Posted 01 November 2008 - 04:34 AM

View Postpriya_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
int n=Integer.parseInt(in.readLine());
while(n > 0)
{
r = n % 10;
n = n / 10;
if(r!= 0 && r!= 1)
{
System.out.println("This is not a binary number.!!!");
System.out.println("Please try once again.!!!");
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");
	int n=Integer.parseInt(int.readLine());

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



View Postpbl, 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!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1