In this article we learn the Number Format Exception In Java, NumberFormatException Example, NumberFormatException Causes and How to handle Number Format Exception in Java.
Number Format Exception In Java
The number format exception in java is an unchecked exception that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, The NumberFormatException is thrown when we try to convert a string into a numeric value such as integer or float. For example, this exception occurs if a string is attempted to be parsed to an integer but the string contains a boolean value. It is a subclass of IllegalArgumentException and implements the Serializable interface.
NumberFormatException is an unchecked exception, it does not need to be declared in throws clause of a method or constructor. It can be handled in code using a try-catch block.
Number Format Exception Constructors
NumberFormatException(): This constructs a NumberFormatException with no specified detailed message
NumberFormatException(String s): This constructs a NumberFormatException with a detailed specified message in string s.
Number Format Exception Example
Number Format Exception in Java Causes
Since NumberFormatException is caused by an inappropriate string format for the corresponding argument of the method throwing the exception, it can have various paths. Some of them are mentioned below:
1. Input string is null
2. Input string is empty
3. Input string having trailing space
Example- Integer.parseInt(“123 “);
4. Input string having a leading space
Example- Integer.parseInt(” 123″);
5. Input string is alphanumeric
6. Input string exceed the range of the datatype storing the parsed string
Example- Integer.parseInt(“140”); The maximum possible value of integer can be 127, but the value in the string is 140 which is out of range, so this will throw the exception.
7. Mismatch between the input string and the type of the method which is being used for parsing. If you provide the input string like “2.0” and you try to convert this string into an integer value, it will throw a NumberFormatException exception.
How to handle Number Format Exception in Java?
NumberFormatException is basically caused because the input string is not properly formatted or illegal when parsing it into a numeric value. Therefore, to avoid this exception, the provided input string should be well formatted.
To have a valid and well-formed string, first check whether the input string is null or not. Then, check for unnecessary spaces and trim them all and then make several checks that the argument string matches the type of the method you’re using to parse the string. If the method is ParseInt(), check that the string contains an integer value and so on for all other methods.
To prevent a Java program from generating a NumberFormatException, it is always a good practice to enclose lines of code that can throw this exception in a try-catch block, as shown below:
Invalid string in argumment