2

For example, I need to grab an unknown number, let's say 3, and find the binary (2^3) - 1 times, from 0 to 111 (0-7). Obviously, the number of digits I need depends on whatever number 'n' in 2^n.

So, if the number is 3, I would need the output to be:

000
001
010
011
100
101
111

Now obviously I can do this manually with a String.format("%03d", NumberInBinary) operation, but that's hardcoding it for 3 digits. I need to do the equivalent code with an unknown number of digits, how can I do that? (as in String.format("%0nd", yournumber) where n is the number of digits.)

1

3 Answers 3

5

if n = 4, NumberInBinary = 101;

String.format("%0"+n+"d", NumberInBinary);

with output

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

5 Comments

this doesn't cover the enumeration and binary conversion aspect.
also, I don't see what NumberInBinary would be for this to work.
@Matthew read the question completly first. n and NumberInBinary are already available for the OP. he just needs to print it.
@Matthew just output binary value, not find.
StackOverflowException is correct, I have already found the binary number. And his solution worked perfectly, I had no idea you could do that, and it makes perfect sense since the inside of the quotes are treated as a String, so doing "%0"+n+"d" did the trick perfectly, adapting itself to every case. Thanks, brother. And thanks Matthew, you also helped me learn of an alternate method of finding the binary value. I just used a for loop, starting at int i = 0, going up until 2^n - 1, getting 'i', using Integer.toString(i, 2), parsing it, and outputting it using StackOverflowExceptions's method.
1

Why not make use of the already built-in Integer.toBinaryString() and just manually add the zeros using a StringBuilder ?

public static void main(String[] args) {
    int max = 5;
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
        String binary = Integer.toBinaryString(i);
        if (binary.length() > max) {
            break;
        }
        System.out.println( prefixWithZeros(binary, max) );
    }
}

static String prefixWithZeros(String binary, int n) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n - binary.length(); i++) {
        sb.append('0');
    }
    return sb.append(binary).toString();
}

Comments

0

You could use recursion:

public static void enumerate(String prefix, int remaining) {
    if (remaining == 0) {
        System.out.println(prefix);
    } else {
        enumerate(prefix + "0", remaining - 1);
        enumerate(prefix + "1", remaining - 1);
    }
}

and then call enumerate("", numberOfDigits);

faster, using StringBuffer:

public static void enumerate(StringBuffer prefix, int remaining) {
    if (remaining == 0) {
        System.out.println(prefix.toString());
    } else {
        enumerate(prefix.append('0'), remaining - 1);
        prefix.deleteCharAt(prefix.length() - 1);
        enumerate(prefix.append('1'), remaining - 1);
        prefix.deleteCharAt(prefix.length() - 1);
    }
}

and then call enumerate(new StringBuffer(), numberOfDigits);

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.