Java异常无效INT长整型整型、异常、Java、INT

2023-09-04 04:52:25 作者:善解人意

我目前正在开发一个数学的应用,使长计算。每当我键入一个整数,其中超过9位数...的错误(其中...被替换为一个很长的数字):我收到java.lang.NumberFormatException:无效INT。当我输入一个整数,它是小于或等于9位,应用程序运行正常。我需要的输出为一个int(即没有小数位)。不明白为什么错误的发生。

的code这是造成问题的位是:

 意向意图= getIntent();
字符串消息= intent.getStringExtra(MainActivity.NUMBER);
INT INP =的Integer.parseInt(消息);
 

解决方案

对于最大值 INT 2 31 -1,即2,147,483,647。如果你试图解析比数字越大,会抛出异常。

如果您需要处理更大的数字,可以使用对于一般较大范围(高达2 63 -1)或的BigInteger 为任意大小。

50 个 Java 开发常见错误及规避技巧 Part 1

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

解决方案

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.

 
精彩推荐
图片推荐