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 } }