Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent java.lang.NumberFormatException: For input string: "N/A"? [closed]

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?

like image 649
codemaniac143 Avatar asked Sep 10 '13 06:09

codemaniac143


People also ask

What causes Java Lang NumberFormatException?

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.

What is Java Lang NumberFormatException for input string?

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.


1 Answers

"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.

  1. 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!  ... } 
like image 170
Subhrajyoti Majumder Avatar answered Oct 03 '22 03:10

Subhrajyoti Majumder