Well, I was bored so I wanted to make a Binary>Decimal Converter. I set the upper limit of my converter to be a 30-bit number (1073741823) or (0111111111111111111111111111111). The problem I am having is that when I try to parse 111111111111111111111111111111, I get a NumberFormatException. Here is some code:
This code is responsible for checking if the String is a number, than parsing it into an int.
if (checkNumber(input)) {
try {
number = Integer.parseInt(input);
} catch (NumberFormatException ex) {
log(ex.getMessage());
}
} else {
toDecimal();
}
Here is the actual boolean for checking the String.
private static boolean checkNumber(String input) {
for (char c : input.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
This is the output of the NumberFormatException:
java.lang.NumberFormatException: For input string: "111111111111111111111111111111"
I just don't understand why Java would throw that error. Any ideas?
Integer.parseInt(String, 2); was the correct answer.
Long.parseLong