How can I prevent java.lang.NumberFormatException: For input string: N/A?
"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!
...
}
java.lang.NumberFormatException: For input string: 5.3
any other way we can handle this without try and catch?
You need to parse it with the right parse method so that the exception is not thrown.
value can be any thing.
Well if that is literally true, it means that you cannot parse it, except the most trivial sense.
it's not fixed that it will always be float.it can be integer and double too.
Ah ... so when you said "any thing" you didn't mean anything?
There are three possible ways to handle this:
Just read the error code as a string, and don't attempt to parse it.
Ask yourself: do you actually need to treat the error code as a number? What does that buy you? And what if the error code isn't a recognizable number at all? (Will your application be able to cope?)
Parse the error code using
parseDouble
, and represent it as adouble
. The syntax parsed byparseDouble
is the same as forparseFloat
and a superset of what is accepted byparseInt
. Specifically,parseDouble
will happily parse an integer value ... and give you thedouble
representation of that integer.The downside is that for large enough integers, the
double
representation will be imprecise / inaccurate. Depending on the actual code values, this might cause problems.If you must parse it with the right type then the way you avoid try / catch would be to use
Pattern
to implement regexes to test which of the expected formats the error code is in. Then call the correspondingparseXXX
method to parse it.The downside is that you now need three (or more) different variables with types
int
,float
,double
and so on to represent the parsed error code values. And all of the places where you used the error code later in your application will have to deal with that.And of course, you could just use a sequence try-catches.
Related Topics
Appending to an Objectoutputstream
Jcomponents Not Showing Up With Picture Background
How Does a Java Hashmap Handle Different Objects With the Same Hash Code
Get Os-Level System Information
Get Generic Type of Java.Util.List
How to Implement a Single Instance Java Application
How Does the "Final" Keyword in Java Work (I Can Still Modify an Object.)
What Is an Initialization Block
Variable Used in Lambda Expression Should Be Final or Effectively Final
How to Address Unchecked Cast Warnings
How to Get the Separate Digits of an Int Number
Convert a Json String to Object in Java Me
How to Format a Duration in Java (E.G Format H:Mm:Ss)
Maven Shade Javafx Runtime Components Are Missing
How to Override Tostring() Properly in Java
What Do 3 Dots Next to a Parameter Type Mean in Java