Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: want to modify code that converts binary code to decimal numbers

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default want to modify code that converts binary code to decimal numbers

    The code here I have works fine if I just want to ask the user to enter four digits:

    //java application that asks user to input  binary numbers(1 or 0) and convert them  to decimal numbers
     
    import java.util.Scanner; //program uses class scanner
     
    public class binarynumber{
     
    	//main method that executes the java application
     
    	public static void main(String args[]){
     
    		//declares variables
     
    		int digit;
    		int base=2;
            int degree;
            double decimal;
            int binary_zero=0;
            int binary_one=1;
            //create scanner for object input
     
            Scanner input=new Scanner(System.in);
     
            System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
     
           if(digit==1 || digit==0)
               System.out.printf("binary in discimal form is %f",digit*(Math.pow(base,0)));
               double ones_place=digit*Math.pow(base,0);
     
           System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
              if(digit==1 || digit==0)
                 System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,1)));
                 double tens_place=digit*Math.pow(base,1);
             System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
                   digit=input.nextInt();
     
             if(digit==1 || digit==0)
                System.out.printf("binary in decimal form is %f",digit*(Math.pow(base,2)));
                double hundreds_place=digit*Math.pow(base,2);
     
             System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
     
               if(digit==1 || digit==0)
                  System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,3)));
     
                 double thousands_place=digit*Math.pow(base,3);
     
     
            System.out.printf( "Output in decimal form is %f", ones_place+tens_place+hundreds_place+thousands_place);
     
     
     
     
     
     
     
     
     
     
    	}//end main
     
    }//end class
    here is the output of my code when I run the java application:

    Input a digit for a binary number(1 or 0):
    1
    binary in discimal form is 1.000000Input a digit for a binary number(1 or 0):
    0
    binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
    0
    binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
    1
    binary in decimal form is 8.000000Output in decimal form is 9.000000Press any ke
    y to continue . . .
    The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop. What should I do?
    Last edited by noblegas; August 29th, 2014 at 11:43 AM. Reason: =Java removed from end tag


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,058
    My Mood
    Mellow
    Thanks
    184
    Thanked 651 Times in 638 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Is it your code?

    Please post whose ever code it is using code or highlight tags which are explained here.

  3. #3
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    59
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    How many times do you call Input.nextInt()?

    How does the program know how to do what you want it to?

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Quote Originally Posted by GregBrannon View Post
    Is it your code?

    Please post whose ever code it is using code or highlight tags which are explained here.
    Okay, I'll repost my code and question:

    The code here I have works fine if I just want to ask the user to enter four digits:

    //java application that asks user to input  binary numbers(1 or 0) and convert them  to decimal numbers
     
    import java.util.Scanner; //program uses class scanner
     
    public class binarynumber{
     
    	//main method that executes the java application
     
    	public static void main(String args[]){
     
    		//declares variables
     
    		int digit;
    		int base=2;
            int degree;
            double decimal;
            int binary_zero=0;
            int binary_one=1;
            //create scanner for object input
     
            Scanner input=new Scanner(System.in);
     
            System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
     
           if(digit==1 || digit==0)
               System.out.printf("binary in discimal form is %f",digit*(Math.pow(base,0)));
               double ones_place=digit*Math.pow(base,0);
     
           System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
              if(digit==1 || digit==0)
                 System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,1)));
                 double tens_place=digit*Math.pow(base,1);
             System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
                   digit=input.nextInt();
     
             if(digit==1 || digit==0)
                System.out.printf("binary in decimal form is %f",digit*(Math.pow(base,2)));
                double hundreds_place=digit*Math.pow(base,2);
     
             System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number
            digit=input.nextInt();
     
               if(digit==1 || digit==0)
                  System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,3)));
     
                 double thousands_place=digit*Math.pow(base,3);
     
     
            System.out.printf( "Output in decimal form is %f", ones_place+tens_place+hundreds_place+thousands_place);
     
     
     
     
     
     
     
     
     
     
    	}//end main
     
    }//end class
    here is the output of my code when I run the java application:

    Input a digit for a binary number(1 or 0):
    1
    binary in discimal form is 1.000000Input a digit for a binary number(1 or 0):
    0
    binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
    0
    binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
    1
    binary in decimal form is 8.000000Output in decimal form is 9.000000Press any ke
    y to continue . . .
    The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop. What should I do?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,058
    My Mood
    Mellow
    Thanks
    184
    Thanked 651 Times in 638 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Put the necessary code in a loop, usually a do/while() or while() loop, that repeats until the user indicates the input is complete or the correct number and correct data has been retrieved.

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Quote Originally Posted by GregBrannon View Post
    Put the necessary code in a loop, usually a do/while() or while() loop, that repeats until the user indicates the input is complete or the correct number and correct data has been retrieved.
    I rewrote my code to incorporate a while loop:

     
    //java application that asks user to input any binary numbers(1 or 0) and convert them  to decimal numbers
     
    import java.util.Scanner; //program uses class scanner
     
    public class binarynumber{
     
    	//main method that executes the java application
     
    	public static void main(String args[]){
     
    		//declares and initialized  variables
     
    		int digit=1;
    		double base=2;
            int degree=0;
            double total=0;
            double decimal=digit*Math.pow(base,degree); //formula that converts binary digits to decimal numbers
     
     
            Scanner input=new Scanner(System.in);
    System.out.println("Enter a binary digit(one or zero)"); //prompts user for an input
            digit=input.nextInt(); //reads user from input
     
           System.out.printf("Conversion for decimal format is %f",decimal);
     
     
    //while loop continues until user inputs integers that are not 1 or not zero
            while(digit!=1 || digit!=0){
     
    			System.out.println("Enter a binary digit(one or zero)");
    			digit=input.nextInt();
    			decimal=digit*Math.pow(base,degree);
    			degree++;
    			System.out.printf("Conversion for decimal format is %f",decimal); //displays decimal result each time
    			total=total+decimal;// sums up the total number of binary numbers that the user input that have been converted over to decimal numbers
     
    		}
     
    if(digit!=0 || digit!=1)
    		   System.out.printf("the binary digit %f", total); //displays total number of binary numbers that have been converted to decimal numbers after the user exits the while loop
     
     
     
     
    	   }//end main
     
       }//end class

    Here is the output of my code when I execute the code:


    Enter a binary digit(one or zero)
    1
    Conversion for decimal format is 1.000000Enter a binary digit(one or zero)
    1
    Conversion for decimal format is 1.000000Enter a binary digit(one or zero)

    1
    Conversion for decimal format is 2.000000Enter a binary digit(one or zero)
    1
    Conversion for decimal format is 4.000000Enter a binary digit(one or zero)
    1
    Conversion for decimal format is 8.000000Enter a binary digit(one or zero)
    0
    Conversion for decimal format is 0.000000Enter a binary digit(one or zero)
    The problem is, when I input a number that isn't 1 or one, it won't exit the while loop and displayed the total like I want it to. What am I doing wrong?
    Last edited by noblegas; August 29th, 2014 at 11:50 AM.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,058
    My Mood
    Mellow
    Thanks
    184
    Thanked 651 Times in 638 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Please follow Java's naming convention and begin your class names with a capital letter, camel cased thereafter.

    Anytime you have repeated code, there's probably an optimization that can be done. In this case, a do/while() loop would eliminate most or all of the repeated code.

    Think about the logic of your while() condition. When will the whole condition be false?

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Quote Originally Posted by GregBrannon View Post
    Please follow Java's naming convention and begin your class names with a capital letter, camel cased thereafter.

    Anytime you have repeated code, there's probably an optimization that can be done. In this case, a do/while() loop would eliminate most or all of the repeated code.

    Think about the logic of your while() condition. When will the whole condition be false?
    I am only going to incorporate code with while loop since I haven't learned about the the for loop and do while loop yet.

    I have set my while loop to be false when the user enters digits that are not 1 or 0 and I thought the loop would exit and print the total which is outside the loop. Here is a piece of my code that only focuses on the while loop part and everything after :

         while(digits!=-1 ||digit!=0){
     
    			System.out.println("Enter a binary digit(one or zero)");
    			digit=input.nextInt();
    		    decimal=digit*Math.pow(base,degree);
    			degree++;
    			System.out.printf("Conversion for decimal format is %f",decimal); //displays decimal result each time
    			total=total+decimal;// sums up the total number of binary numbers that the user input that have been converted over to decimal numbers
     
    		}//end while
     
     
     
     
    		   System.out.printf("the binary digit %f", total); //displays total number of binary numbers that have been converted to decimal numbers after the user exits the while loop
     
    	   }//end main
     
       }//end class

    when the user enters integers that are not 1 or 0, it the code reads them as digits and incorporates them into the equation for the binary-to-decimal converter instead of printing the total

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    while(digits!=-1 || digit!=0){
    You need to think about how the OR operator (||) works. If either of the sub conditions are true, then the whole condition is true. With the AND operator (&&) all sub conditions must be true for the whole condition to be true.

    What if digits == 0? Then digits !=1 is true and the whole condition returns true.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (August 29th, 2014)

  11. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    I want to make it so that once I used entered the last digit to exit the while loop, the last digit isn't included in the binary to decimal converter formula. Couldn't I just used the while loop without adding any if conditional statements?

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    couldn't I just used the while loop
    I was talking about the boolean expression inside the ()s that's part of the while statement.
    What I said would also be true of the condition expression used in an if statement or anywhere else a boolean condition is used.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    Quote Originally Posted by Norm View Post
    I was talking about the boolean expression inside the ()s that's part of the while statement.
    What I said would also be true of the condition expression used in an if statement or anywhere else a boolean condition is used.
    I haven't learned boolean yet. I tried adding a charter string where I said n!='n' to exist out inside the while curly brackets , but that didn't some to work. Couldn't I just incorporate teh while loop without any if statements

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,207 Times in 2,180 Posts

    Default Re: want to modify code that converts binary code to decimal numbers

    I haven't learned boolean yet.
    You're using boolean expressions in the while statement and if statements.
    X == 2 is a boolean expression. It resolves to a boolean value of true or false.
    ((x == 2) && (y > 3)) is (what I call) a compound boolean expression: two simple expressions joined by a boolean operator like AND or OR. again it resolves to a boolean value

    Couldn't I just incorporate teh while loop without any if statements
    I don't know what if statement you are referring to. What would be its purpose?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. manual code for converting binary octa hexa and decimal.
    By aycaveron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 25th, 2014, 11:22 AM
  2. Modify Hub code to Switch
    By Prince Boothe in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2014, 07:50 AM
  3. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  4. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 06:30 AM
  5. Having Trouble Modify the code!!
    By jehov86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 09:42 AM