无法在Java中将String转换为Integer [英] Cannot convert String to Integer in Java
本文介绍了无法在Java中将String转换为Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个将字符串转换为整数的函数
I have written a function to convert string to integer
if ( data != null )
{
int theValue = Integer.parseInt( data.trim(), 16 );
return theValue;
}
else
return null;
我有一个字符串,它是6042076399,它给了我错误:
I have a string which is 6042076399 and it gave me errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6042076399"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
这不是将字符串转换为整数的正确方法吗?
Is this not the correct way to convert string to integer?
推荐答案
以下是我喜欢的方式:
编辑(08/04/2015):
Edit (08/04/2015):
如下面的评论中所述,这实际上更好地完成了这个:
As noted in the comment below, this is actually better done like this:
String numStr = "123";
int num = Integer.parseInt(numStr);
这篇关于无法在Java中将String转换为Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文