gpt4 book ai didi

java - 将字符串转换为整数时出现数字格式异常

转载 作者:行者123 更新时间:2023-11-29 04:28:51 25 4
gpt4 key购买 nike

我正在编写将字符串中给定的数字转换为整数的代码。但它给出了超过 9 位数字的数字格式异常。有没有其他方法可以做到这一点。

public class StringToInt {

public static void main(String args[])
{
try
{
long test =Integer.valueOf("9007199254");
System.out.println("num :"+test);
}catch(NumberFormatException e)
{
System.out.println("Error..."+e.toString());
}

}

最佳答案

But it gives Number Format Exception for more than 9 digit number.

考虑使用 long 类型,因为您的数字大于整数最大范围 (2,147,483,647)。

long test =Long.valueOf("9007199254"); 

关于java - 将字符串转换为整数时出现数字格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44732656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com