Javatpoint Logo
Javatpoint Logo

NumberFormatException in Java

The NumberFormatException is thrown when we try to convert a string into a numeric value such as float or integer, but the format of the input string is not appropriate or illegal. By illegal format, it is meant that if you are trying to parse a string to an integer but the String contains a boolean value, it is of illegal format. For example - if we try to parse a string to integer but the string is null.

It is an unchecked exception. It is a subclass of IllegalArgumentException and implements the Serializable interface.

Constructors of NumberFormatException

Constructor Description
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.

Example of NumberFormatException

Output:

NumberFormatException in Java

Common reasons for NumberFormatException

Since NumberFormatException occurs due to the inappropriate format of string for the corresponding argument of the method which is throwing the exception, there can be various ways of it. A few of them are mentioned as follows-

  • The input string provided might be null-
    Example- Integer.parseInt(null);
  • The input string might be empty-
    Example- Integer.parseInt("");
  • The input string might be having trailing space-
    Example- Integer.parseInt("123 ");
  • The input string might be having a leading space-
    Example- Integer.parseInt(" 123");
  • The input string may be alphanumeric-
    Example- Long.parseLong("b2");
  • The input string may have an input which might exceed the range of the datatype storing the parsed string-
    Example- Integer.parseInt("135"); The maximum possible value of integer can be 127, but the value in the string is 135 which is out of range, so this will throw the exception.
  • There may be a mismatch between the input string and the type of the method which is being used for parsing. If you provide the input string like "1.0" and you try to convert this string into an integer value, it will throw a NumberFormatException exception.
    Example- Integer.parseInt("1..0");

How to avoid NumberFormatException?

The NumberFormatException is basically caused because the input string is not well formatted or illegal while parsing into a numerical value. So, to avoid this exception, the input string provided has to be well formatted.

To have a valid and well-formatted string, first of all, check whether the input string is not null. Then, check for unnecessary spaces and trim out all of them after that put several checks to verify that argument string matches the type of the method which we are using for parsing the string. If the method is ParseInt(), check that the string is has an integer value and likewise for all other methods do the required checks.

In order to prevent a Java program from generating a NumberFormatException, it is always a good practice to enclosed the lines of code which can throw this exception in a try-catch block as shown below-


Next TopicJava Tutorial




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA