Java NumberFormatException

NumberFormatException occurs when trying to convert a string into number.

Java NumberFormatException

NumberFormatException occurs when trying to convert a string into number.

Java NumberFormatException

Java NumberFormatException

In this section we will read about the exception named "NumberFormatException" thrown by the application.

NumberFormatException is an exception thrown by any application when the application is tried to convert a String into a number i.e. Numeric type. Such exception is thrown because the attempt for converting the number into a numeric type is not in the appropriate format.

Let's discuss this exception in detail using a scenario i.e. why we need to convert a string into number and why the NumberFormatException is thrown. Suppose I have created an application for adding the two numbers by taking inputs through command line arguments. As we all know the inputs in Java are taken in the String format. In this situation if we give the input a number it treats the input as a String representation of number. And if we do not convert this input programmatically into a specified numeric types then the application can not perform the arithmetic operations with these inputs. So, a programmer needs to convert the string representation of a number programmatically into the specified numeric types. Now, we will discuss why the NumberFormatException is thrown. According to the scenario what we have discussed earlier that an addition application requires the numeric types to perform the mathematical add operation on the two numbers. And the Java treats every input as a string. In such condition we need to convert the input into a numeric type. Java has provided the methods for converting the string representation number to the specified numeric types e.g. Integer.parseInt(), Double.parseDouble(), etc. In such methods you have to provide the numeric values only. If you provides the alphabets/special characters/characters in place of numeric values then these methods will throw the NumberFormatException.

NumberFormatException is a runtime exception that checks the input for the specified format at runtime. NumberFormatException class belongs to the java.lang package so, to use the this class there is no need to import any package.

This class has two constructors which are used to create object of NumberFormatException. These are as follows :

  • NumberFormatException() : This constructor is used to create an object without any message detail.
  • NumberFormatException(String s) : This constructor is used to create an object with the specified message passed as an argument.

Example

Here I am giving a simple example which will demonstrate you about how to use the NumberFormatException in Java. Here you will read about how to catch the NumberFormatException if it is thrown by the application. In this example I will create a simple Java class for adding two integer numbers. In this example the input will be taken from the user at run time. Then if the inputs will be appropriate then it will produce the accurate answer otherwise the NumberFormatException would be thrown.

Example

public class NumberFormatExceptionExample {

	public static void main(String args[])
	{	                           
		try{
		int firstNumber = Integer.parseInt(args[0]);
		int secondNumber = Integer.parseInt(args[1]);
		if(firstNumber >= 0 && secondNumber >= 0)
		{
			int add = firstNumber+secondNumber;
			System.out.println("Addition Is : "+add);
		}		
		}
		catch(NumberFormatException nfe)
		{
			System.out.println("Not A Number");
			nfe.printStackTrace();
		}
		
	}
}

Output

When you will compile and execute the above example with giving the appropriate numeric type values then the output will be as follows :

But, if you will compile and execute the above example by giving the inappropriate value i.e. except than a numeric type values then the output will be as follows :

Download Source Code