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?
lang. NumberFormatException comes when you try to parse a non-numeric String to a Number like Short, Integer, Float, Double etc. For example, if you try to convert . "null" to an integer then you will get NumberFormatException.
The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java.
"N/A"
is not an integer. It must throw NumberFormatException
if you try to parse it to an integer.
Check before parsing or handle Exception
properly.
Exception Handling
try{ int i = Integer.parseInt(input); } catch(NumberFormatException ex){ // handle your exception ... }
or - Integer pattern matching -
String input=...; String pattern ="-?\\d+"; if(input.matches("-?\\d+")){ // any positive or negetive integer or not! ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With