relyt
relyt

Reputation: 679

Binary to character matrix help

FYI: This is a practice homework exercise. I've been working on it, but now I'm stuck. Any tips/help would be appreciated. I've been staring at it a while and no progress has been made.

Summarized question: Nine coins are place in a 3x3 matrix with some face up and some face down. Heads = 0 and tails = 1. Each state can also be represented using a binary number. There are 512 possibilities. Problem: Write a program that asks user for a number between 0-511 and displays corresponding matrix with characters H and T like this:

User enters number 7 (which is 000000111 or HHHHHHTTT) Display should be: H H H H H H T T T

This is what I have so far. I'm not asking for the answer necessarily, I would just like a push in the right direction. Thanks

import java.util.Scanner;

public class converting {
    public static void main(String[] ar) {

    Scanner s = new Scanner(System.in);

        System.out.print("Enter a number between 0 and 511: ");

        int number = s.nextInt();
        if(number <= 511 && number > 0)
        {
             String bin = Integer.toBinaryString(number);
             String tails = bin.replace('1', 'T');

         int count = 0;
         char[] arr = tails.toCharArray();

         for (int i = 0; i < arr.length; i++) {

            System.out.print(arr[i]);
            count++;
            if (count == 3) {
                System.out.println();
                count = 0;
            }
        }
      }
      else{
        System.out.print("Please enter a number between 0 and 511\n");
    }
    }
}

Upvotes: 1

Views: 1471

Answers (4)

Maarten Bodewes
Maarten Bodewes

Reputation: 11

  • I would use StringBuilder to build up the "board", and only print it out afterwards.
  • I would create a method called "createBoard" that has as parameters the number, the width and the depth. Always program for flexibility.
  • I would use two counters, say x and y that iterate through the width and depth (a simple for next loop suffices). position = x * width + y
  • I would use BigInteger.valueOf() and use BigInteger.testBit(position).
  • I would make really sure that my code looks nice and has comments in.
  • Upvotes: 1

    T.J. Crowder
    T.J. Crowder

    Reputation: 1075079

    You're really close. Some notes:

    • Scanner#nextInt can throw exceptions; handle them gracefully.
    • You need to check that number is in range (0-511).
    • Once you have bin, you have 1-9 binary digits -- 0s and 1s. You want to make sure you have exactly 9 of them, so insert any missing 0s at the front.
    • You now have 9 0s and 1s; you want Hs and Ts. Check out String#replace.
    • You now have a string with 9 Hs and Ts. Output it in three lines, three chars per line. Check out String#substring.

    Upvotes: 4

    finnw
    finnw

    Reputation: 48639

    String.replace(CharSequence, CharSequence) may be useful here.

    Upvotes: 1

    Anon.
    Anon.

    Reputation: 60013

    intToString.toCharArray();

    This should be bin.toCharArray(), methinks.

    What else are you having problems with?

    Upvotes: 1

    Related Questions