我收到以下错误:
Caused by: java.lang.NumberFormatException: Invalid int: "1437061569000"
完整错误here。
我已检查字符串是否为null,应用trim()和replace()方法删除任何非整数字符。
错误行(42)是在字符串上运行parseInt方法的行。
代码在这里:
@Override
protected String doInBackground(String... college) {
String data_url = "......";
int lastUpdateTimestamp;
try {
String data = DownloadText(data_url).trim().replaceAll( "[^\\d]", "" );
Log.e("doinback", data);
if (data == null) { Log.e("doinback", "ITS NULL"); }
lastUpdateTimestamp = Integer.parseInt(data);
res = "done";
} catch (IOException e) {
res = "ERROR";
}
return res;
}
答案 0 :(得分:4)
这个数字太大了。 int可以具有的最大值是2 ^ 31 - 1.请参阅here。
java.lang.Integer中
<强> MAX_VALUE 强>
public static final int MAX_VALUE
保持
int
最大值的常数,2 ^ 31-1。
2 ^ 31 - 1 = 2 147 483 647
答案 1 :(得分:3)
Int数据类型是32位带符号的二进制补码整数。
最小值为 - 2,147,483,648。( - 2 ^ 31)
最大值为2,147,483,647(含)。(2 ^ 31 -1)
1.437.061.569.000
比最大值更大。
您需要使用长变量。
长数据类型是64位带符号的二进制补码整数。
最低值为-9,223,372,036,854,775,808。( - 2 ^ 63)
最大值为9,223,372,036,854,775,807(含)。 (2 ^ 63 -1)
请阅读http://www.tutorialspoint.com/java/java_basic_datatypes.htm
答案 2 :(得分:1)
为什么不使用long
?
由于NumberFormatException
值超限,因此抛出了Integer
。 Integer
的限制为-2,147,483,648
至2,147,483,647
。
如果您想将此号码(1437061569000
)用作lastUpdateTimestamp
变量,则可以将其转换为long
,其限制大于Integer
,即来自-9,223,372,036,854,775,808
至9,223,372,036,854,775,807
。