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

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


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


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


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