-1

My goal is to create a simple binary to decimal calculator. I try to go about this by first having the user input a string of the binary value they are trying to calculate and later use the length of this string to run a for loop (as seen in the code below). The calculator appears to work fine but fails when the user enters a binary number (of all 1's) longer than 20 digits. I receive a java.util.InputMismatchException error and I don't know how to fix it.

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a binary number to convert to decimal: ");
        long binaryNum = scan.nextLong();
        System.out.println(binaryConverter(binaryNum));
        scan.close();

}

    public static long binaryConverter(long binaryNum) {
        String binaryString = Long.toString(binaryNum);
        long decimalValue = 0;
        for(int i = 0; i < binaryString.length(); i++) {
            if((binaryNum%10) == 0) {
                binaryNum = binaryNum/10;
            } else if((binaryNum%10) == 1) {
                decimalValue += Math.pow(2, i);
                binaryNum = binaryNum/10;
            } else {
                System.out.println("This isn't a binary number. Please try again.");
                break;
            }
        }   
        return decimalValue;
}
}
Reza
  • 7
  • 1
  • Possible duplicate of [A long bigger than Long.MAX\_VALUE](https://stackoverflow.com/questions/16546038/a-long-bigger-than-long-max-value) – Progman May 04 '19 at 20:55
  • `Long` can't take more than 19 digits – Akash Jain May 04 '19 at 20:57
  • Don't use `nextLong()` then `Long.toString()`. Use `next()` to get a string directly, so you don't have length limitations – Andreas May 04 '19 at 20:57
  • You can use `scan.nextBigInteger()` to get bigger integer values. – Progman May 04 '19 at 20:58
  • According to a binary to decimal calculator online, a binary number with 20 digits is 1048575. As far as I know, longs can hold much larger numbers than that. – Reza May 04 '19 at 21:02
  • You are not reading a `binary` number. You are are reading a `long` decimal value that is composed of `1's`. You should read the binary number in as a String of `1's` and `0's` and work with that. Another alternative is to use `scanner.nextLong(2)` where `2` is the radix of the number. – WJS May 04 '19 at 21:03
  • Oh ok, this makes sense. – Reza May 04 '19 at 21:13

2 Answers2

2

The way you want to do this to use scanner.nextLong(2) where 2 is the radix. Then you will be reading in an actual binary number.

long number = scanner.nextLong(2);
System.out.println(number);

produces

144115188075855871

for input of

111111111111111111111111111111111111111111111111111111111
WJS
  • 36,363
  • 4
  • 24
  • 39
1

If I understood you correctly you always want to convert the binary input to a decimal value. A very simple solution would look like this:

public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  System.out.println("Please enter a binary number to convert to decimal: ");
  final String input = scan.next();
  System.out.println(Integer.parseInt(input, 2));
  scan.close();
}

If you are interested how it works under the hood, take a look at the java source for Integer.parseInt.

Daniel P.
  • 369
  • 3
  • 12