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 4 of 4

Thread: Binary Multiplier only working for certain cases

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Multiplier only working for certain cases

    I am trying to create a program that scans two numbers from the user, multiplies them, and then prints the binary result. As of now, my program can multiply binary numbers and output a decimal number. I will get to the rest later. However, the program only works with certain cases. I think it is having trouble recognizing some of the 0's that are inputted. Here it is below:
    import java.util.Scanner;
    public class BinaryMulitplier {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter a binary number");
    		int x = sc.nextInt(); //first input value
    		System.out.println("Enter another binary number");
    		int y = sc.nextInt(); //second input value
    		String Strx = Integer.toString(x); //converts first input value to String
    		String Stry = Integer.toString(y); //converts second input value to String
    		int lengthx = Strx.length();
    		int lengthy = Stry.length();
    		double decx = 0;
    		double decy = 0;
    		double finDecx = 0;
    		double finDecy = 0;
    		for(int i = lengthx - 1; i >= 0; i--) {
    			char charx = Strx.charAt(i); //
    			if (charx == '0') {
    				decx = 0; //if a digit is 0, it will not go into the conversion
    			}
    			else if (charx == '1') {
    				decx = Math.pow(2, i); //if a digit is 1, it will take 
    			}					//2^(numerical placement in the overall number
    			finDecx = finDecx + decx; //consecutively adds the numbers from above
    		}
    		for(int j = lengthy - 1; j >= 0; j--) {
    			char chary = Stry.charAt(j); //
    			if (chary == '0') {
    				decy = 0;
    			}
    			else if (chary == '1') {
    				decy = Math.pow(2, j);
    			}
    			finDecy = finDecy + decy;
    		}
    		double prod = finDecx * finDecy; //multiplies the two dec numbers
    		System.out.println(prod); //prints the product
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary Multiplier only working for certain cases

    Can you copy the contents of the console and paste it here for the test cases that are not working?
    Be sure to add some comments like: <<<<<<< Should be 23 to show the problems.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Multiplier only working for certain cases

    For example, when I put in 11(3) and 11(3), the output is 9 which is correct. However, if I put in 1010(10) and 111(7), the output is 35 when it should be 70.

    --- Update ---

    For example, when I put in 11(3) and 11(3), the output is 9 which is correct. However, if I put in 1010(10) and 111(7), the output is 35 when it should be 70.
    Also, if I put in 101(5) and 101(5), it outputs 25 which is good. However, when I put in 1010(10) and 1010(10), I also get 25. I don't think it is recognizing the final 0 and I don't know why.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary Multiplier only working for certain cases

    How are you trying to debug the code?
    I use print statements. Add some print statements to the code that print out the intermediate results so you can see what the program is doing.
    The print out will show you what is happening and help you fix it.

    I don't think it is recognizing the final 0
    What should the code do when it sees a final 0?

    The code needs some comments describing what it is trying to do and how it is going to do it.
    For example what is the purpose of the two identical loops.

    The two loops should be one method that can be called twice.

    Where is the logic for the binary multiply? This expression uses the * operator with two double operands:
    finDecx * finDecy;
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Ignoring cases
    By noel222 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 11th, 2012, 06:34 PM
  2. Referring to cases
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2011, 07:41 AM
  3. Switching letter-cases from user input; why is not working?
    By Kimiko Sakaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 27th, 2011, 07:48 AM
  4. Multiplier
    By r-veras in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2011, 09:07 AM
  5. Converting Cases Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 27th, 2010, 12:07 PM