Java Reference ยท java.lang
Java Integer.parseInt()
Reference for Java Integer.parseInt(): convert strings to int, use other radixes, handle NumberFormatException, signs, whitespace and range limits.
Integer.parseInt() converts a String containing a decimal number into a primitive int. It is the standard way to read numbers from user input, files, command-line arguments and configuration values.
The parser is strict. The text must contain only an optional sign followed by digits, with nothing else: no spaces, no thousands separators, no decimal point, no trailing letters. Anything else, and any value outside the int range, throws a NumberFormatException. A second overload accepts a radix so you can parse binary, octal, hexadecimal and up to base 36.
int n = Integer.parseInt("42"); int mask = Integer.parseInt("ff", 16); // 255
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
static int parseInt(String s) static int parseInt(String s, int radix) static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) // Java 9
| Name | Description |
|---|---|
s |
The text to parse. A leading - or + is allowed. |
radix |
The base, from Character.MIN_RADIX (2) to Character.MAX_RADIX (36). Defaults to 10. |
beginIndex, endIndex |
The part of the CharSequence to parse, avoiding a substring() call. |
Returns: the int value.
Throws: NumberFormatException if s is null, empty, contains a non-digit for the radix, or the value does not fit in an int. The radix overload also throws it for an invalid radix. The range overload throws IndexOutOfBoundsException for bad indexes.
Usage notes
- No trimming.
" 12"fails. Callstrip()first if the source may contain whitespace. nullgives aNumberFormatException, not aNullPointerException, with the messageCannot parse null string.- Digits only. Underscores,
0xprefixes and decimals are not accepted. UseInteger.decode()for0x/0prefixes, andDouble.parseDouble()followed by a cast (ornew BigDecimal(text)) for decimals. - Unicode digits. Digits from other scripts, such as Arabic-Indic digits, are accepted because the parser uses
Character.digit(). - Large values. Use
Long.parseLong()ornew BigInteger(text)when numbers may exceed 2,147,483,647. valueOfversusparseInt.Integer.valueOf(String)parses too but returns a (possibly cached)Integer. UseparseIntwhen you want the primitive.- No built-in "try" variant. Java has no
tryParse, so wrap the call as shown, or validate with a regular expression. Do not use exceptions for control flow in a tight loop over millions of values; they are slow when thrown often. - Locale-independent.
parseIntignores the default locale. For localized input such as1.234meaning one thousand, useNumberFormat. - Unsigned input.
parseUnsignedIntaccepts values up to 4294967295 and returns the corresponding bit pattern.