如何防止java.lang.NumberFormatException:用于输入字符串:“N/A”?在运行我的代码时,我得到了一个NumberFormatException:java.lang.NumberFormatException: For input string: "N/A"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)`如何防止此异常发生?
3 回答

慕桂英4014372
TA贡献1871条经验 获得超13个赞
"N/A"
NumberFormatException
Exception
异常处理*
try{ int i = Integer.parseInt(input);}catch(NumberFormatException ex){ // handle your exception ...}
String input=...;String pattern ="-?\\d+";if(input.matches("-?\\d+")){ // any positive or negetive integer or not! ...}

收到一只叮咚
TA贡献1821条经验 获得超4个赞
N/A
int
NumberFormatException
.
String str="N/A"; try { int val=Integer.parseInt(str); }catch (NumberFormatException e){ System.out.println("not a number"); }

慕妹3146593
TA贡献1820条经验 获得超9个赞
private int ConvertIntoNumeric(String xVal){ try { return Integer.parseInt(xVal); } catch(Exception ex) { return 0; }}....int xTest = ConvertIntoNumeric("N/A"); //Will return 0
添加回答
举报
0/150
提交
取消