Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The NumberFormatException class

Reference for java.lang.NumberFormatException: which parse methods throw it, what the messages look like, how to validate input and avoid it.

java.lang.NumberFormatException is an unchecked exception thrown when a method that converts a String to a number receives text that does not have the required format. It extends IllegalArgumentException, so a single catch (IllegalArgumentException e) also catches it.

The usual sources are Integer.parseInt(), Long.parseLong(), Double.parseDouble(), new BigDecimal(String) and new BigInteger(String). The exception tells you that the text is wrong, not that your code has a bug: user input, files and network data can all contain garbage, so you should expect it and handle it.

Java
public class NumberFormatException extends IllegalArgumentException

Try it

Java What triggers it and what it says

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Validating input and falling back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public NumberFormatException()
public NumberFormatException(String s)
Constructor Description
NumberFormatException() No message.
NumberFormatException(String s) Detail message, available through getMessage().

Everything else (getMessage(), getCause(), printStackTrace()) is inherited from Throwable.

Usage notes

  • Methods that throw it: Integer.parseInt, Integer.valueOf(String), Integer.decode, the same methods on Long, Short, Byte, Double, Float, plus the BigDecimal and BigInteger string constructors. Boolean.parseBoolean() never throws; it returns false for anything that is not "true" (case-insensitive).
  • Messages differ by cause. A bad string gives For input string: "abc"; an empty string gives For input string: ""; a value that is well formed but too large for int gives the same style of message (For input string: "99999999999" in the example above). Do not parse messages; they are for humans.
  • No trimming. Leading or trailing spaces make parseInt fail. Call strip() first when the data may be padded.
  • Format rules are strict: an optional + or - followed by digits only. No thousands separators, no decimal point for integer types, no 0x prefix (use Integer.decode() or a radix argument).
  • Locale. Double.parseDouble("1,5") fails because the parse methods always use the English decimal point. Use NumberFormat or DecimalFormat for locale-aware input.
  • null is different: Integer.parseInt(null) also throws NumberFormatException (message Cannot parse null string), while Double.parseDouble(null) throws NullPointerException.
  • Avoid exceptions for control flow in hot loops. Creating the exception and its stack trace is far slower than checking the text first. For bulk validation, test characters yourself or use a precompiled Pattern.
  • Catch it close to the input. Convert it into a friendly message or a default value at the boundary where the text enters your program, not deep inside business logic.