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)`
How can I prevent this exception from occurring?
Obviously you can't parse
N/A
toint
value. you can do something like following to handle thatNumberFormatException
.Integer.parseInt(str) throws
NumberFormatException
if the string does not contain a parsable integer. You can hadle the same as below."N/A" is a string and cannot be converted to a number. Catch the exception and handle it. For example:
"N/A"
is not integer. It must throwNumberFormatException
if you try to parse it to integer.Check before parsing. or handle
Exception
properly.or - Integer pattern matching -
Make an exception handler like this,