--- title: NumberFormatException in Java- Scaler Topics description: Learn about NumberFormatException in detail on Scaler Topics with all examples and all the programs involved in it; read to know more. category: Java author: Mansi --- :::section{.abstract} `NumberFormatException` in Java is an unchecked exception, a subclass of `IllegalArgumentException`. This class implements a serializable interface. It occurs while converting a String with an improper format to other numeric forms like Integer or float. A `parseInt()` method converts a string to an integer type when operating with the string. parseInt() method can be used in two ways, as given below: ```java // This function parses the string argument as a signed decimal integer public static int parseInt(String s) throws NumberFormatException //This function parses the string argument as a signed integer in the radix specified public static int parseInt(String s, int radix) throws NumberFormatException ``` Both methods convert a string to an integer. The only difference between these **`two syntaxes`** is the parameter radix. When **`radix=10`**, the first method is considered equivalent to the second method. ::: :::section{.main} ## Constructors of NumberFormatException | Constructor | Description | | :--------: | :--------: | | public NumberFormatException() | default constructor, an exception, NumberFormatException with no description. | |public NumberFormatException(String msg)|Constructs an exception, NumberFormatException, with a detailed description of it, parameterized, with an error message .| ::: :::section{.main} ## Common Reasons for NumberFormatException There are various problems related to improper string formatting. Here are a few important reasons for NumberFormatException in java: * **Null input string:** ```java Integer.parseInt("null"); ``` * **Input string which is empty:** ```java Float.parseFloat(“”); ``` * **Leading or trailing white spaces in input string:** ```java Integer abc=new Integer(“ 123 “); ``` * **Extra symbol in input string:** ```java Float.parseFloat(4,123); ``` * **Input string with data which is non-numeric:** ```java Double.parseDouble(“FifityFive”); ``` * **Alphanumeric input string:** ```java Integer.valueOf(“33.three”); ``` * **Input string with exceeded range of datatype:** ```java Integer.parseInt(“1826589745243”); ``` * **Type mismatch between the value of the input string and its method:** ```java Integer.parseInt("12.33"); ``` ::: :::section{.main} ## Example of NumberFormatException Program to illustrate **`NumberFormatException`** in java. In this example, we will try to understand how and why NumberFormartException occurs. ```java import java.util.Scanner; public class Test { // Main method public static void main(String[] arg) { // variable for holding the input int number; // scanner class for taking input from the user Scanner sc = new Scanner(System.in); //If the condition is true, it means run the loop until the integer is valid while (true) { System.out.println("Enter an Integer: "); // Try block to catch the exception try { // Parsing the input of user number = Integer.parseInt(sc.next()); // prints if the number is valid System.out.println("You entered: " + number); // out of loop break; } // Catch block for handling NumberFormatException in java catch (NumberFormatException e) { System.out.println( "NumberFormatException occurred"); } } } } ``` **Output**: ```java Enter an Integer: 15 You entered: 15 ``` ```java Enter an Integer: 13.3 NumberFormatException occurred Enter any valid Integer: 13.2 NumberFormatException occurred Enter an Integer: 15 You entered: 15 ``` **Explanation**: As per the code, as soon as the user enters a number other than an Integer, like **`double or float`**, the NumberFormatException occurs. **Flow of the given code:** * Input is taken from `user` * The input is parsed using `parseInt` * If there is an `error` while parsing * `NumberFormatException` is thrown * Else `Output` is printed ::: :::section{.main} ## How to Handle NumberFormatException? The NumberFormatException in java can be handled in **`two`** ways: 1. Using the **`block try-catch** : This involves surrounding the code that might cause the exception with a try block and handling the exception in a catch block. For example: ```java class Main { public static void main(String args[]) { try { // parse the string "12a" to an integer int number = Integer.parseInt("12a"); System.out.println("Number: " + number); } catch (NumberFormatException e) { // catch the exception if the string is not a valid integer System.out.println("Wrong data format: " + e.getMessage()); } } } ``` **Output**: ```java Wrong data format: For input string: "12a" ``` 2. Using the `throws` keyword: This involves declaring that a method might throw a specific exception and leaving it to the calling method to handle it. For example, a method parsing a string to an integer might declare that it throws a `NumberFormatException` if the string is not valid. The calling method would then have to handle this exception using a `try-catch` block or declare that it throws the exception using the `throws` keyword. For example: ```java class Main { public static int parseInt(String str) throws NumberFormatException { return Integer.parseInt(str); } public static void main(String args[]) { try { //Call the parseInt method and pass the string "12a" as argument int number = parseInt("12a"); System.out.println("Number: " + number); } catch (NumberFormatException e) { // catch the exception if the string is not a valid integer System.out.println("Wrong data format: " + e.getMessage()); } } } ``` **Output**: ```java Wrong data format: For input string: "12a" ``` ::: :::section{.summary} ## Conclusion * `NumberFormatException` in java is thrown while converting an improperly formatted string to a numeric value like an integer or float. * It is an unchecked exception known as a run-time exception. The `try-catch block` handles this exception. * Constructors of NumberFormatException: * public NumberFormatException() * public NumberFormatException(String msg) * The NumberFormatException in java can be handled by using a try-catch block or using the throws keyword :::