[英]Getting A NumberFormat Exception in BlackBerry
I am trying to parse a string to int value. 我试图将字符串解析为int值。 But i am getting a NumberFormat Exception. 但我得到一个NumberFormat例外。 I am writing the below code: 我正在编写以下代码:
Logger.out("Myprof", "Contact "+strContact);
try{
i = Integer.parseInt(strContact.trim());
Logger.out("Myprof", "Contact8686866 "+i);
}
catch(Exception e)
{
Logger.out("Myprof", "exce "+e.toString());
}
Now when i am passing like below: 现在当我像下面这样经过:
i = Integer.parseInt("11223344");
I am getting the i value as 11223344. 我得到i值为11223344。
Where i am doing wrong here? 我在哪里做错了? Please Help. 请帮忙。
The input value of 9875566521
is greater than Integer.MAX_VALUE of 2147483647
. 输入值9875566521
大于Integer.MAX_VALUE为2147483647
。 Instead use a Long
. 而是使用Long
。 ( BigInteger
not an option for Blackberry) ( BigInteger
不是Blackberry的选项)
Long number = Long.parseLong(strContact);
Logger.out("Myprof", "Contact8686866 " + number);
If the intended input numbers are greater then Long.MAX_VALUE
, then Character.iDigit can be used as an alternative to validate values: 如果预期的输入数字大于Long.MAX_VALUE
,则可以使用Character.iDigit作为验证值的替代方法:
private static boolean isValidNumber(String strContact) {
for (int i = 0; i < strContact.length(); i++) {
if (!Character.isDigit(strContact.charAt(i))) {
return false;
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.