Functional extraction
Your method does not do toBinary
, it does:
- read input from user
- convert to binary
- print back for user
When you have methods that do multiple things it becomes hard to isolate and reuse the logic. Your code should be split in to parts, and the toBinary
method should take the int
input and return a String
Complicated end-of-loop handling
The while
condition while (num >= 2) {
is a complicated construct, that needs a comment. That logic is key to the process, and guarantees that you are left with a "dangling" 1 for the high-bit, and also handles the 0
input case. It took me a number of minutes to figure out how that code works, and that's not useful for your readers... (or yourself, a year in the future).
As it happens, a do-while loop is the structure you want, and it simplifies the logic a whole bunch....
valueOf(...)
mystery
I assume you have done a static import of the String
class? Otherwise the valueOf
method is not defined. Doesn't this defeat some of your "old-school" desire? Also, why not just use a simple 2-value lookup table. The digits are either 0
or 1
, so it's simply:
char[] digits = new char[]{'0', '1'};
and then your valueOf
can be replaced with:
digits[num % 2]
implicit type conversion
You do an implicit type conversion in the output num + newS
which is not "old-school" either. Adding a number to a string is... complicated.
Unnecessary reversals
Why do the reverse mechanism? Why not just build the digit string up backwards to start with? Instead of:
s = s + valueOf(num % 2);
have:
s = valueOf(num % 2) + s;
String concatenation
String concatenation with the +
operator is often frowned on... in this instance, it makes the code cleaner, and I am leaving it in. The alternative is to use a StringBuilder
, but that has other issues in this use-case. Like you, I would use string concatenation...
Conclusion
All told, I would recommend something like:
private static final char[] digits = "01".toCharArray();
private static String toBinaryRL(int num) {
if (num < 0) {
throw new IllegalArgumentException("toBinary can only handle positive integers, not " + num);
}
String binary = "";
do {
binary = digits[num % 2] + binary;
num /= 2;
} while (num > 0);
return binary;
}
Further, this process can be easily extended in to any "base", by extending the digits
, and adding a parameter to the function:
private static final char[] digits = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
private static String toBase(int num, int base) {
if (num < 0) {
throw new IllegalArgumentException("toBase can only handle positive integers, not " + num);
}
if (base <= 1 || base >= digits.length) {
throw new IllegalArgumentException("Cannot convert to base " + base);
}
String result = "";
do {
result = digits[num % base] + result;
num /= base;
} while (num > 0);
return result;
}
private static String toBinaryB(int num) {
return toBase(num, 2);
}
x
used for? \$\endgroup\$