Skip to content
Tutorials Center
My learning

Topics

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.

Java
int n = Integer.parseInt("42");
int mask = Integer.parseInt("ff", 16); // 255

Try it

Java Parsing and failing

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 Radixes and safe parsing

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
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. Call strip() first if the source may contain whitespace.
  • null gives a NumberFormatException, not a NullPointerException, with the message Cannot parse null string.
  • Digits only. Underscores, 0x prefixes and decimals are not accepted. Use Integer.decode() for 0x/0 prefixes, and Double.parseDouble() followed by a cast (or new 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() or new BigInteger(text) when numbers may exceed 2,147,483,647.
  • valueOf versus parseInt. Integer.valueOf(String) parses too but returns a (possibly cached) Integer. Use parseInt when 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. parseInt ignores the default locale. For localized input such as 1.234 meaning one thousand, use NumberFormat.
  • Unsigned input. parseUnsignedInt accepts values up to 4294967295 and returns the corresponding bit pattern.