CodeGym/Java Blog/Java Exceptions/NumberFormatException in Java
Author
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

NumberFormatException in Java

Published in the Java Exceptions group
members

What is NumberFormatException in Java?

“The NumberFormatException is thrown at the conversion of a non-numeric String to a number.”
Simply put, if a String does not hold only numeric data and you try to convert it into a number, you will encounter this exception. This can also be interpreted in another way. The exception is used as an indicator if the conversion of String to a number is not possible. The number can be an integer, float or decimal as per your requirements. For example, if the input string contains all letters, alpha-numeric characters or special characters and you try to attempt a conversion from String to Integer then a NumberFormatException will be thrown.

Example

Let’s look at a simple example to understand this.
import java.util.Date;

public class NumberFormatExceptionTestDriver {

	public static void main(String[] args) {

		try {
			// a valid Integer number in the input String
			String inputString = "2550";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			// java.lang.NumberFormatException will be thrown if the
			// input string can not be converted to a valid integer
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// a floating number in the input String
			// use Float.parseFloat(inputString) to avoid this exception
			String inputString = "255.0";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			Date day = new Date();
			// date containing alpha-numeric data in the input string
			String inputString = day.toString();
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// numbers with spaces in the input string
			String inputString = "1 2 3";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// all letters in the input string
			String inputString = "Lubaina Khan";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));

		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}
	}
}

Output

Integer.pareseInt(2550) = 2550 NumberFormatException thrown! For input string: "255.0" NumberFormatException thrown! For input string: "Wed Aug 11 08:18:21 PKT 2021" NumberFormatException thrown! For input string: "1 2 3" NumberFormatException thrown! For input string: "Lubaina Khan"

Conclusion

We hope you understand why NumberFormatException arises in Java. If you have understood the root cause, you can always identify the reason and rectify it. If you are still not clear about this concept, make more mistakes and you will learn the whys and hows of this problem. Till then keep growing and keep learning.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet