Question

I'm currently developing a math application that makes long computations. I'm getting java.lang.NumberFormatException: Invalid int: "..." error (where the ... is replaced by a very long number) whenever I type an integer that contains more than 9 digits. When I type in an integer that is less than or equal to 9 digits, the application runs fine. I need the output to be an int (i.e. no decimal places). Not quite sure why the error's occurring.

The bit of code that's causing the problem is:

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.NUMBER);
int inp = Integer.parseInt(message);
Was it helpful?

Solution

The maximum value for an int is 231-1, i.e. 2,147,483,647. If you try to parse a larger number than that, the exception will be thrown.

If you need to handle larger numbers, either use long for a generally larger range (up to 263-1) or BigInteger for an arbitrary size.

OTHER TIPS

Java's int datatype is limited to the values of -2,147,483,648 to 2,147,483,647 (inclusive). If you want larger 'integer' values I'd recommend using the long datatype.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top