Open In App
Related Articles

NumberFormatException in Java with Examples

Improve Article
Save Article
Like Article

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value.  That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java. It is a subclass of IllegalArgumentException class.  To handle this exception, trycatch block can be used.

While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt().

Usage of parseInt() method: As we already know there are two variants of this method namely as follows to get a better understanding 

public static int parseInt(String s) throws NumberFormatException

This function parses the string argument as a signed decimal integer.
public static int parseInt(String s, int radix) throws NumberFormatException

This function parses the string argument as a signed integer in the radix specified by the second argument.

Return Type:

In simple words, both methods convert the string into its integer equivalent. The only difference being is that of the parameter radix. The first method can be considered as an equivalent of the second method with radix = 10 (Decimal).


  1. public NumberFormatException(): Constructs a NumberFormatException with no detail message.
  2. public NumberFormatException(String msg): Constructs a NumberFormatException with the detail message ‘msg’

Common Reasons for NumberFormatException:

There are various issues related to improper string format for conversion in numeric value. A few of them are:

1. The input string is null

Integer.parseInt("null") ;

2. The input string is empty

Float.parseFloat(“”) ; 

3. The input string with leading and/or trailing white spaces

Integer abc=new Integer(“  432 “);

4. The input string with extra symbols


5. The input  string with non-numeric data


6. The input string is alphanumeric


7. The input string might exceed the range of the datatype storing the parsed string


8. Data type mismatch between input string value and the type of the method which is being used for parsing




// Java Program to illustrate NumberFormatException
// Importing Scanner class to take
// input number from the user
import java.util.Scanner;
//  Class
public class GFG {
    // Main driver method
    public static void main(String[] arg)
        // Declaring an variable which
        // holds the input number entered
        int number;
        // Creating a Scanner class object to
        // take input from keyboard
        // -> Keyboard
        Scanner sc = new Scanner(;
        // Condition check
        // If condition holds true, Continue loop until
        // valid integer is entered by user
        while (true) {
            // Display message
            System.out.println("Enter any valid Integer: ");
            // Try block to check if any exception occurs
            try {
                // Parsing user input to integer
                // using the parseInt() method
                number = Integer.parseInt(;
                // Number can be valid or invalid
                // If number is valid, print and display
                // the message and number
                System.out.println("You entered: "
                                   + number);
                // Get off from this loop
            // Catch block to handle NumberFormatException
            catch (NumberFormatException e) {
                // Print the message if exception occurred
                    "NumberFormatException occurred");

Output: The below output is for different numbers been entered by the user 

Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
NumberFormatException occurred
Enter any valid Integer:
You entered: 1000

Last Updated : 18 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials