Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串转换为整数时获取数字格式异常_Java_Numberformatexception - Fatal编程技术网

Java 将字符串转换为整数时获取数字格式异常

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

我正在编写一个代码,用于将字符串中给定的数字转换为整数。但对于超过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());
    }

}
}

但对于超过9位数的数字,它给出了数字格式例外

考虑使用
long
类型,因为您的数字大于整数最大范围(2147483647)


int
limit是
2147483647
这就是为什么它会给出
NumberFormatException


尝试使用
long

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

尝试使用
long
long也会给出相同的异常
long。值
not
Integer
int
限制直到
2147483647
其工作时为止。。。。。thanxError…java.lang.NumberFormatException:对于输入字符串:“9007199254”@parimal非左侧,右侧也必须更改。
long test =Long.valueOf("9007199254");