0

So I'm writing this program that simulates a coinflip with sides H and T, and its come time to create my method. The main idea behind the program is that a user should be able to enter any number b/w 1 and 511. Once the number is entered, my method should convert their number to binary, 0's being Heads, and 1's being Tails.

So if the user enters, 3, my methos should convert to 000000011, and then I want to convert it to print a matrix that looks like:

  HHH
  HHH
  HTT

Here is my code so far, however my method (binaryConverter) is empty.I really have no clue where to begin.

import java.util.Scanner;
public class Problem8_11 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter one number between 0 and 511: ");
    int number = input.nextInt();

    String binaryValue = binaryConverter(number);

    int[][] matrix = new int[3][3];
    int binary = 0;

    for (int i = 0; i < matrix.length; i++) {
            for (int x = 0; x < matrix[i].length; x++) {
            int HeadOrTails = (binaryValue.charAt(binary++) == '0') ? 0 : 1;
                    matrix[x][i] = HeadOrTails;
        }
    }
    for (int i = 0; i < matrix.length; i++) {

        for (int x = 0; x < matrix[i].length; x++) {
            char HorT = (matrix[i][x] == 0) ? 'H' : 'T';
            System.out.print(HorT + "");
        }
        System.out.println(" ");
    }

}
tcann99
  • 3
  • 3
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – Federico klez Culloca Feb 21 '18 at 16:37
  • Why is this tagged with `javascript`? – AP. Feb 21 '18 at 16:38
  • I'm not sure how to begin this, thats why I made the post. I really have no clue where to start. You dont have to write it for me, nor did I ask anyone to write it for me. I asked for SUGGESTIONS, on where to start. Thanks for trying tho. – tcann99 Feb 21 '18 at 16:43
  • 1
    You don't need to use a string, just test the bottom bit of your number (`x & 1 != 0`) for the `0`/`1` or `H`/`T` and use a shift right (`x = x >> 1`) to move to th enext bit. – OldCurmudgeon Feb 21 '18 at 16:43
  • This is already answered here: https://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java – Leni Feb 21 '18 at 16:52

2 Answers2

1

You don't need to convert to string - that is wasteful.

Use simple bit testing.

private boolean test(int n, int bit) {
    return ((1 << bit) & n) != 0;
}

private String test(int n, int bit, String t, String f) {
    return test(n, bit) ? t : f;
}

public void test(String[] args) {
    int n = 3;
    for (int bit = 8; bit >= 0; bit--) {
        System.out.print(test(n, bit, "H", "T"));
    }
}

TTTTTTTHH

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

If your problem is just on how to convert an int to a binary string, that is already answered in below question: Converting an int to a binary string representation in Java?

You need to append any leading zeros of course. So your binary converter can do something like below:

String convertToBinaryString(int i){
    String bString = Integer.toBinaryString(i);

    while(bString.length() < 12){
        bString = "0" + bString;
   }

   return bString ;
}
Leni
  • 653
  • 1
  • 6
  • 23
  • So i tried adding it, but now i seem to get an error with the code. – tcann99 Feb 21 '18 at 17:51
  • Sorry, I'v missed () in bString.length(). Corrected now. – Leni Feb 22 '18 at 15:55
  • @tcann99 You can just replace `String binaryValue = binaryConverter(number);` in your code with `String binaryValue = Integer.toBinaryString(i); while(bString.length() < 12){ bString = "0" + bString; }` – Leni Feb 22 '18 at 16:04