0

I have to take a user input of integers from a range, convert that to binary, and fill a 3x3 array with the binary. The only problem is, my code is giving me an output of only dependent on the first 3 numbers of that binary (i.e 010001101 = 010 across all rows).

import java.util.Scanner;
public class HW11P02 { 

    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        System.out.print("Enter a number between 0 and 511: ");
        int n = in.nextInt();


        String binary = Integer.toBinaryString(n);
        binary = binary.format("%09d", Integer.parseInt(binary));
        System.out.println(binary);

        listArray(binary);


    };

    public static String[][] listArray(String binary) {
        String[][] array = new String[3][3];

        char ch = ' ';
        String value = "";

        for (int i = 0; i < 3; i++) {
            for (int n = 0; n < 3; n++) {
                ch = binary.charAt(n);
                value = Character.toString(ch);
                array[i][n] = value;
                System.out.print(array[i][n] + " ");
            }
            System.out.println();
        }



        return array;
    }
};
5
  • You have to remember that this will not gonna print leading 0's in binary if there is no need to: ex: number 2 is gonna be: 10, because there is no need to use more than 2 bits. Enter number 255 and repost me what you get as a result. Commented Dec 3, 2016 at 6:30
  • @Slay29 my results were 0 1 1 across all 3 rows Commented Dec 3, 2016 at 6:38
  • Try using long instead normal int. Is there any difference? Commented Dec 3, 2016 at 6:40
  • @Slay29 tested it, still the same output. Commented Dec 3, 2016 at 6:44
  • I suggest you learn some debugging skills. Use a debugger to step through your code and examine the values of variables. This will let you see exactly what your code is doing and where your logic may be incorrect. Commented Dec 3, 2016 at 6:47

1 Answer 1

1

I think this will provide the the output you may really want.

import java.util.Scanner;

public class HW11P02 {

public static void main(String[] args)
{
    Scanner in = new Scanner (System.in);
    System.out.print("Enter a number between 0 and 511: ");
    int n = in.nextInt();

    String binary = Integer.toBinaryString(n);
    binary = binary.format("%09d", Integer.parseInt(binary));
    System.out.println(binary);

    int result[][]=new int[3][3];
    int position=0;
    for (int i = 0; i < result.length; i++)
    {
        for (int j = 0; j < result.length; j++)
        {
            result[i][j]=binary.charAt(position++)-'0';
            System.out.print(result[i][j]+" ");
        }
        System.out.println();
    }


}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.