在java中将字符串转换为整数值时出现NumberFormatException
当我试图将input1字符串解析为整数值时,它会引发NumberFormatException。我尝试替换字符串中的空格(如果有的话),但它对我无效在java中将字符串转换为整数值时出现NumberFormatException,java,exception,numberformatexception,Java,Exception,Numberformatexception,当我试图将input1字符串解析为整数值时,它会引发NumberFormatException。我尝试替换字符串中的空格(如果有的话),但它对我无效 int number = 0; String input1 = "12345354987"; try{ ip = Integer.parseInt(input1); }catch(NumberFormatException e){ System.out.println("not a number"); }
int number = 0;
String input1 = "12345354987";
try{
ip = Integer.parseInt(input1);
}catch(NumberFormatException e){
System.out.println("not a number");
}
整数最大范围为
2147483647
,您正在越过它。您可以使用Long或BigDecimal来表示大数字。这是因为字符串中的数字超出了整数范围,如果确实需要,请使用BigDecimal来表示如下大值:
String input1 = "12345354";
BigInteger ib =new BigInteger(input1);
System.out.println(ib);
或
对于适合长范围的小值,您可以使用:
String input1 = "1234535455";
Long ip = Long.parseLong(input1);
System.out.println(ip);
您的数字大于整数最大值。请使用long,然后尝试解析。您是否尝试查看堆栈跟踪中的e?异常通常包含非常有用的信息。@Eran谢谢,它对我很有用。。!看。最大值=2_147_483_647
String input1 = "1234535455";
Long ip = Long.parseLong(input1);
System.out.println(ip);