title: Formatting binary output from an array in Java
body: <p>Here's my code: <a href="http://pastebin.com/x8XKSufP" rel="nofollow">http://pastebin.com/x8XKSufP</a></p>
  
  <p>I need to format the output into proper binary format. For example, if you enter a decimal "64" I need it to output "0100 0000", NOT 1000000. I've been trying to figure this out for over an hour. Please help.</p>
  
  <p>Test.java</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Test {
     public static int[] getBinaryArray(int decimal) {
        int result = decimal;
        int length = 0;
        while (result != 0) {
           result /= 2;
           length++;
        }
        return new int[length];
     } // close getBinaryArray method
  
     public static int[] decimalToBinary(int[] binary, int decimal) {
        int result = decimal;
        int arrayLength = binary.length - 1;
        while (result != 0) {
           if (result % 2 == 0) {
              binary[arrayLength] = 0;
           } else {
              binary[arrayLength] = 1;
           }
           arrayLength--;
           result /= 2;
        } // close WHILE
        return binary;
     } // close decimalToBinary method
  
     // Main method
     public static void main(String[] args) {
        // initialize the input scanner
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number in decimal format: ");
        int getDecimal = input.nextInt();
        int[] binary = decimalToBinary(getBinaryArray(getDecimal), getDecimal);
        for (int i = 0; i &lt; binary.length; i++) {
           System.out.println(i + " " + binary[i]);
        } // close FOR
     } // main method
  }
  </code></pre>