Converting Decimal to Binary Java

I am trying to convert decimal to binary numbers from the user's input using Java.

I'm getting errors.

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

How do I convert Decimal to Binary in Java?


Solution 1:

Integer.toBinaryString() is an in-built method and will do quite well.

Solution 2:

Integer.toString(n,8) // decimal to octal

Integer.toString(n,2) // decimal to binary

Integer.toString(n,16) //decimal to Hex

where n = decimal number.

Solution 3:

Your binaryForm method is getting caught in an infinite recursion, you need to return if number <= 1:

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));
            printBinaryform(number);
        }
    }

    private static void printBinaryform(int number) {
        int remainder;

        if (number <= 1) {
            System.out.print(number);
            return; // KICK OUT OF THE RECURSION
        }

        remainder = number % 2;
        printBinaryform(number >> 1);
        System.out.print(remainder);
    }
}

Solution 4:

I just want to add, for anyone who uses:

   String x=Integer.toBinaryString()

to get a String of Binary numbers and wants to convert that string into an int. If you use

  int y=Integer.parseInt(x)

you will get a NumberFormatException error.

What I did to convert String x to Integers, was first converted each individual Char in the String x to a single Char in a for loop.

  char t = (x.charAt(z));

I then converted each Char back into an individual String,

  String u=String.valueOf(t);

then Parsed each String into an Integer.

Id figure Id post this, because I took me a while to figure out how to get a binary such as 01010101 into Integer form.

Solution 5:

/**
 * @param no
 *            : Decimal no
 * @return binary as integer array
 */
public int[] convertBinary(int no) {
    int i = 0, temp[] = new int[7];
    int binary[];
    while (no > 0) {
        temp[i++] = no % 2;
        no /= 2;
    }
    binary = new int[i];
    int k = 0;
    for (int j = i - 1; j >= 0; j--) {
        binary[k++] = temp[j];
    }

    return binary;
}