I’m almost finished writing code that allows a user to enter a decimal number into binary, have the binary value reversed, and then convert it back to decimal.
Except for the Binary to Decimal phase, I solved everything. No matter what I type, it continues returning the same number to me. I’m not sure why I attempted to grasp this blog but it didn’t assist me. Simply put, why is the last section of my code incorrect? I’m having difficulties determining the length of the array/string and then multiplying it by 2n, etc.
package reversedBinary; import java.util.Scanner; public class ReversedBinary { public static void main(String[] args) { int number; Scanner in = new Scanner(System.in); System.out.println("Enter a positive integer"); number=in.nextInt(); if (number <0) System.out.println("Error: Not a positive integer"); else { System.out.print("Convert to binary is:"); System.out.print(binaryform(number)); } } private static Object binaryform(int number) { int remainder; if (number <=1) { System.out.print(number); } remainder= number %2; binaryform(number >>1); System.out.print(remainder); { return null; } } }