views:

77

answers:

4

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");
    }
    }
}
+1  A: 

intToString.toCharArray();

This should be bin.toCharArray(), methinks.

What else are you having problems with?

Anon.
+1  A: 

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

finnw
+4  A: 

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.
T.J. Crowder
Thanks for the answer. I added a check for valid numbers if(number <= 511 My question now is how can I replace all F's with 0's. My char array is currently char[] arr = tails.toCharArray(); which only has the replaced 1's. Does that make sense?Is there a way to update my first post with the code I have now?
relyt
Ok. Updated first post. Thanks!
relyt
@relyt: Shouldn't that `> 0` be `>= 0`? Or is the user not allowed to have a board that's all `H`s?
T.J. Crowder
+1  A: 

  • 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.
  • Maarten Bodewes
    Maarten, I'm sure this is probably a good way to do it, but at this point I think it's over my head. As I learn more this may make sense to me. Thanks for the response though.
    relyt