Java异常无效INT长整型 [英] Java exception invalid int for long integer

查看:301
本文介绍了Java异常无效INT长整型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个数学的应用,使长计算。每当我键入一个整数,其中超过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 为任意大小。

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.

这篇关于Java异常无效INT长整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆