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.
public class NumberFormatException extends IllegalArgumentException
Try it
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.
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
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 onLong,Short,Byte,Double,Float, plus theBigDecimalandBigIntegerstring constructors.Boolean.parseBoolean()never throws; it returnsfalsefor anything that is not "true" (case-insensitive). - Messages differ by cause. A bad string gives
For input string: "abc"; an empty string givesFor input string: ""; a value that is well formed but too large forintgives 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
parseIntfail. Callstrip()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, no0xprefix (useInteger.decode()or a radix argument). - Locale.
Double.parseDouble("1,5")fails because the parse methods always use the English decimal point. UseNumberFormatorDecimalFormatfor locale-aware input. nullis different:Integer.parseInt(null)also throwsNumberFormatException(messageCannot parse null string), whileDouble.parseDouble(null)throwsNullPointerException.- 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.