Java 将字符串转换为整数时获取数字格式异常
我正在编写一个代码,用于将字符串中给定的数字转换为整数。但对于超过9位数的数字,它给出了数字格式例外。有没有其他方法可以做到这一点Java 将字符串转换为整数时获取数字格式异常,java,numberformatexception,Java,Numberformatexception,我正在编写一个代码,用于将字符串中给定的数字转换为整数。但对于超过9位数的数字,它给出了数字格式例外。有没有其他方法可以做到这一点 public class StringToInt { public static void main(String args[]) { try { long test =Integer.valueOf("9007199254"); System.out.println("num :"+test); }
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());
}
}
}
但对于超过9位数的数字,它给出了数字格式例外
考虑使用long
类型,因为您的数字大于整数最大范围(2147483647)
int
limit是2147483647
这就是为什么它会给出NumberFormatException
尝试使用
long
long test =Long.valueOf("9007199254");
尝试使用
long
long也会给出相同的异常long。值notInteger
int
限制直到2147483647
其工作时为止。。。。。thanxError…java.lang.NumberFormatException:对于输入字符串:“9007199254”@parimal非左侧,右侧也必须更改。
long test =Long.valueOf("9007199254");