Java exception invalid int for long integer

2020-05-09 00:43发布

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);

2条回答
相关推荐>>
2楼-- · 2020-05-09 01:19

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.

查看更多
淡お忘
3楼-- · 2020-05-09 01:29

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.

查看更多
登录 后发表回答