我尝试了这段代码并得到了一个异常,任何人都可以帮忙。即使这样,传递给函数的值是“12”,我也会得到异常。我尝试过带/不带修剪等,
公共(public)类测试类{
public static void main(String[] args) {
try{
// TODO Auto-generated method stub
String line = "12-10-1984";
String [] temp = line.split("-");
String t1 = temp[0].trim();
Integer.parseInt(t1);
}catch (Exception e){
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
最佳答案
您可以尝试像这样过滤字符串...
String[] temp = line.split("-");
String t1 = temp[0].trim();
StringBuilder sb = new StringBuilder();
for (char ch : t1.toCharArray()) {
if (Character.isDigit(ch)) {
sb.append(ch);
}
}
if (sb.toString().length() > 0) {
int v = Integer.valueOf(sb.toString());
}
关于java - parseInt 中的 NumberformatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778781/