Binary to Decimal

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2621 Views - Last Post: 05 February 2010 - 02:29 PM Rate Topic: -----

#1 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
		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




Is This A Good Question/Topic? 0
  • +

Replies To: Binary to Decimal

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • 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.
int n=Integer.parseInt(in.readLine());



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

Was This Post Helpful? 1
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Binary to Decimal

Posted 02 February 2010 - 08:39 PM

View Posterik.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...
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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.out.println("Please try once again.!!!");
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

Was This Post Helpful? 0
  • +
  • -

#6 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.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
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • 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.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
 
             // Need ending brackets!
         } // End of main()
} // End of class.



That should allow it to at least compile.
Was This Post Helpful? 0
  • +
  • -

#9 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.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
 
             {}// Need ending brackets!
         } // End of main()
} // End of class


Was This Post Helpful? 0
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#12 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.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
 
             {}// Need ending brackets!
         } // End of main()
} // End of class


Was This Post Helpful? 0
  • +
  • -

#13 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#14 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • 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 {
//.....


Was This Post Helpful? 0
  • +
  • -

#15 ThisGirly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.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
 
             {}// Need ending brackets!
         } // End of main()
} // End of class



thanks erik
thanks japanir

binary integers* srry
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2