如何防止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 回答

宝慕林4294392
TA贡献2021条经验 获得超8个赞
"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贡献1806条经验 获得超5个赞
N/A
int
NumberFormatException
.
String str="N/A"; try { int val=Integer.parseInt(str); }catch (NumberFormatException e){ System.out.println("not a number"); }

皈依舞
TA贡献1851条经验 获得超3个赞
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
提交
取消