本文介紹了如何防止 java.lang.NumberFormatException:對于輸入字符串:“N/A"?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在運行我的代碼時,我得到一個 NumberFormatException
:
While running my code I am getting 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)`
如何防止此異常發生?
推薦答案
"N/A"
不是整數.如果您嘗試將其解析為整數,它必須拋出 NumberFormatException
.
"N/A"
is not an integer. It must throw NumberFormatException
if you try to parse it to an integer.
解析前檢查或正確處理Exception
.
Check before parsing or handle Exception
properly.
異常處理
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!
...
}
這篇關于如何防止 java.lang.NumberFormatException:對于輸入字符串:“N/A"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!