java.lang.NumberFormatException for Input String

The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format.

For example, if someone converts the string “Tutorial & Example” into a number, the java.langNumberFormatException is raised, as it is not possible to convert the given string into a number.

The NumberFormatException is the child of the IllegalArgumentnException class. The exception also implements the Serializable interface. Note that itis an unchecked exception.

Constructors of the NumberFormatException

ConstructorDescription
NumberFormatException()Constructs an object of the NumberFormatException class with no specific message.
NumberFormatException(String s)Constructs an object of the NumberFormatException class with the specific message mentioned in the string s.

The following shows when the NumberFormatException is raised.

FileName: NumberFormatExecptionExample.java

Output:

Common Cause for the NumberFormatException  

We have already seen in the above example that when there is irrational string format. There can be various ways that makes the input string inappropriate for the conversion into numbers. Some of the ways are discussed below.

  • When the input string is empty. For example, int i = Integer.parseInt(“”);
  • When the input string is null. For example, int i = Integer.parseInt(null);
  • When the input string contains leading blank spaces. For example, int i = Integer.parseInt(“  143”);
  • When the input string contains trailing blank spaces. For example, int i = Integer.parseInt(“133  ”);
  • When the input string contains alphabets as well as numbers. For example, int i = Integer.parseInt(“abc23”);
  • When the input string contains numbers only, but the number overflows. For example, int i = Integer.parseInt(“14455455423456”); In this example, the input string such a huge number that an integer cannot hold.
  • When the input string contains number only, but the number underflows. For example, int i = Integer.parseInt(“-14455455423456”); In this example, the input string such a small number that an integer cannot hold.
  • When there is a mismatch between the method used for parsing and the input number.

For example, int i = Integer.parseInt(“14.455”); In this example, the input string is of type float or double. However, the parse method used here is of type int. Hence, the exception is raised.

Avoiding the NumberFormatException 

We have seen the various scenarios where the exception NumberFormatException is raised. Therefore, one needs to handle the exception as it is common to put inappropriate string for parsing. The following program shows how to handle the NumberFormatException.

NumberFormatExceptionExample1.java

Output:


Explanation: The line of code at which there is a chance of occurrence of NumberFormatException should be enclosed within a try-catch block. Thus, when the exception is raised, it can be handled in the catch block.

Pin It on Pinterest

Share This