Java

NumberFormatException in Java

The developers in Java often need to work with multiple sorts of data formatted differently. For instance, accumulating randomly generated or encrypted data in the code. In such situations, coping with the “NumberFormatException” in Java is assistive in refraining from the limitations, thereby streamlining the code functionalities at the developer’s end.

This blog will elaborate on facing and coping with the “NumberFormatException” in Java.

What is “NumberFormatException” in Java?

The “NumberFormatException” corresponds to an exception in Java when an attempt is done to transform a string having an incorrect format into a numeric value. The conversion is done via the “parseInt()” method. This method fetches the primitive data type of the “String”.

Syntax

parseInt(x, y)

In this syntax:

  • x” refers to the string representation of decimal.
  • y” transforms “x” into an integer.

Ideal Case

The following example explains the ideal case scenario to transform the string into an integer with no limitations:

int x = Integer.parseInt("30");

System.out.println("The integer is: "+x);

In the above code, apply the below-provided steps:

  • First, specify the type “Integer” in which the desired conversion is required.
  • Now, apply the “parseInt()” method to parse the specified string to an integer.
  • This will give the “integer” representation of the specified string.

Output

In this output, it can be observed that the “integer” representation of the string is retrieved accordingly.

Reasons For the Faced “NumberFormatException” in Java

The “NumberFormatException” in Java can be encountered for multiple reasons. Let’s go through each of the reasons one by one.

Case 1: Empty Input String

In this case, the discussed exception can be faced due to an “empty” string passed as the “parseInt()” method’s parameter that needs to be converted into an integer:

int x = Integer.parseInt("");

System.out.println(x);

Output

Case 2: Null Input String

In this scenario, the exception can be faced due to the specified “null” keyword as a method parameter:

int x = Integer.parseInt(null);

System.out.println(x);

Output

Note: The exception statement is identical in both the above cases since the value as the method’s parameter is considered “null”.

Case 3: Input String With Additional Symbols

Including extra symbols in the string can also result in the discussed exception, as follows:

System.out.println(Integer.parseInt("1,23"));

Output

Case 4: Non-Numeric Input String

A non-numeric string, i.e., “Thirty Five” in this case can also cause the “NumberFormatException” to occur:

int x = Integer.parseInt("Thirty Five");

System.out.println(x);

Output

Case 5: Input String Exceeding the Range

The maximum limit for the “parseInt()” method is to accumulate “11” characters. The range exceeding this number can also display the discussed error, as faced below:

int x = Integer.parseInt("123452347684933");

System.out.println(x);

Output

Case 6: Input String With Alphanumeric Data

In this situation, the stated limitation can be faced due to the contained alphabets and numbers simultaneously being transformed into an integer:

int x = Integer.parseInt("Thirty 1");

System.out.println(x);

Output

Case 7: Mismatched Data Type

The mismatch in the “return” type and the specified “data type” can also result in encountering the discussed limitation:

int x = Integer.parseInt("30.6");

System.out.println(x);

Note that in this case, the return type is “Integer,” but the specified data type is “float”, i.e., 30.6.

Output

Case 8: Input String With Padded Whitespaces

The padded whitespaces at the start or end can also be a reason for such an error to be faced:

int x = Integer.parseInt(" 30 ");

System.out.println(x);

Output

This was all about the faced limitation in different scenarios. Now, let’s discuss the approach to eliminate the discussed limitation.

How to Handle “NumberFormatException” in Java?

The discussed “NumberFormatException” limitation in Java can be catered using the “try…catch” statement by catching the faced exception in the “catch” block:

try {

int x = Integer.parseInt(" 30 ");

System.out.println(x);

}

catch (NumberFormatException e) {

System.out.println("Exception catered!");

}

System.out.println("Resuming execution...");

In this code snippet, apply the following steps:

  • First, specify the “try” block/statement.
  • In this block, include the return type, i.e., “Integer” and apply the “parseInt()” method to transform the specified string into an “integer” and display it.
  • Note that the bottleneck(s) faced while executing the “try” block can be faced by the “catch” block.
  • The probable exception, i.e., “NumberFormatException” can now be placed in the “catch” block.
  • Within this block, display the stated message instead of the faced exception.
  • Out of the “catch” block, display the stated message indicating that the functionalities will resume regardless of the encountered exception.

Output

From this outcome, it can be implied that the exception has been coped with appropriately.

Conclusion

The “NumberFormatException” in Java is faced when an attempt is made to transform a string with an incorrect format into an integer. This exception can be caused due to multiple discussed reasons, i.e., padded whitespaces in the string, exceeded string limit, etc., and can be coped via the “try…catch” statements. This blog discussed the approaches to facing and coping with the “NumberFormatException” in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.