99

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?

hichris123
  • 10,145
  • 15
  • 56
  • 70
codemaniac143
  • 1,241
  • 2
  • 11
  • 18

6 Answers6

117

"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!
 ...
}
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • 1
    yes you are right I forgot to assign it to integer value now i got the error and yes it is resolved successfully thank you – codemaniac143 Sep 10 '13 at 06:42
  • +1 for the integer pattern matching. I wasn't sure how to do a loop reading a line from stdin using a try/catch. – Jason Hartley Nov 12 '14 at 08:29
  • @JasonHartley, please review the answer. I have edited it and explained why you would not prefer to use integer pattern matching in your code. – LPVOID Jul 12 '17 at 13:35
  • How to get integer from this type of string "151564" in double quotes – Zeeshan Ali Mar 30 '18 at 11:22
10

Integer.parseInt(str) throws NumberFormatException if the string does not contain a parsable integer. You can hadle the same as below.

int a;
String str = "N/A";

try {   
   a = Integer.parseInt(str);
} catch (NumberFormatException nfe) {
  // Handle the condition when str is not a number.
}
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
9

Make an exception handler like this,

private int ConvertIntoNumeric(String xVal)
{
 try
  { 
     return Integer.parseInt(xVal);
  }
 catch(Exception ex) 
  {
     return 0; 
  }
}

.
.
.
.

int xTest = ConvertIntoNumeric("N/A");  //Will return 0
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
7

Obviously you can't parse N/A to int value. you can do something like following to handle that NumberFormatException .

   String str="N/A";
   try {
        int val=Integer.parseInt(str);
   }catch (NumberFormatException e){
       System.out.println("not a number"); 
   } 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
6

"N/A" is a string and cannot be converted to a number. Catch the exception and handle it. For example:

    String text = "N/A";
    int intVal = 0;
    try {
        intVal = Integer.parseInt(text);
    } catch (NumberFormatException e) {
        //Log it if needed
        intVal = //default fallback value;
    }
rocketboy
  • 9,573
  • 2
  • 34
  • 36
2

'N/A' cannot be parsed to int and we get the exception and there might a case where the provided string could be <-2147483648 or > 2147483648 (int max and min) and in such case too we get number format exception and in such case we can try as below.

String str= "8765432198";
Long num= Long.valueOf(str);
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
Integer n=0;
if (num > max) {
        n = max;
    }
if (num < min) {
        n = min;
    }
if (num <= max && num >= min)
  n = Integer.valueOf(str);
NKR
  • 167
  • 9