In Java, Integer.parseInt(s) reads the text in s and returns the whole number it describes as a primitive int. It is the standard way to turn user input, file data, or a command-line argument into a number you can calculate with.
The text may start with - or + and must then contain only digits. A second argument chooses the base, so parseInt("ff", 16) reads hexadecimal. Any text that is not a valid int throws a NumberFormatException.
A static method of java.lang.Integer: call it on the class, as Integer.parseInt(...). Integer lives in java.lang, so no import is needed.
Syntax
Pass the text, and optionally the radix (the base of the number).
Java
int a = Integer.parseInt("42"); // 42
int b = Integer.parseInt("-17"); // -17
int c = Integer.parseInt("ff", 16); // 255
Call
What it does
Integer.parseInt(s)
Reads s as a decimal int.
Integer.parseInt(s, radix)
Reads s in the given base, from 2 to 36.
Integer.parseInt(s.trim())
Removes surrounding spaces first, because spaces are not allowed.
Integer.valueOf(s)
Same parsing, but returns an Integer object.
parseInt accepts no spaces, no decimal point, and no thousands separators. Java 9 adds a form that reads a slice of a CharSequence; the examples here stay on Java 8.
Library declarations
Java
// java.lang.Integer
public static int parseInt(String s) throws NumberFormatException
public static int parseInt(String s, int radix) throws NumberFormatException
// Java 9 and later
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
Parameters
Parameter
Description
s (String)
The text to read. It must not be null or empty.
radix (int)
The base of the number, from Character.MIN_RADIX (2) to Character.MAX_RADIX (36). The one-argument form uses 10.
Return Value
Return Value
Description
int
The number the text describes, between Integer.MIN_VALUE and Integer.MAX_VALUE.
Accepts values up to 4294967295 and wraps them into an int.
Double.parseDouble(s)
Needed when the text has a decimal point.
Java Examples
Example 1 — Turn Text Quantities into a Total
Parse a few quantities written as text, including a sign, and add them up.
Java
public class IntegerParseIntDemo {
public static void main(String[] args) {
String[] quantities = {"3", "12", "-4", "+8"};
int total = 0;
for (String text : quantities) {
int value = Integer.parseInt(text);
System.out.println("\"" + text + "\" -> " + value);
total += value;
}
System.out.println("total: " + total);
}
}
One call per string. The loop hands each piece of text to parseInt, which returns a real int.
Signs are fine."-4" becomes -4, and "+8" becomes 8 because a leading plus is accepted.
Now it is arithmetic. Because the values are int values, total += value adds them as numbers, not as joined text.
The sum. 3 + 12 - 4 + 8 gives 19.
Example 2 — Read Binary, Octal, Hex, and Base 36 Text
Pass a radix as the second argument to read numbers that are not written in base 10.
Java
public class IntegerParseIntRadix {
public static void main(String[] args) {
System.out.println("1010 in base 2: " + Integer.parseInt("1010", 2));
System.out.println("777 in base 8: " + Integer.parseInt("777", 8));
System.out.println("ff in base 16: " + Integer.parseInt("ff", 16));
System.out.println("FF in base 16: " + Integer.parseInt("FF", 16));
System.out.println("zz in base 36: " + Integer.parseInt("zz", 36));
System.out.println("-101 in base 2: " + Integer.parseInt("-101", 2));
}
}
Output
1010 in base 2: 10
777 in base 8: 511
ff in base 16: 255
FF in base 16: 255
zz in base 36: 1295
-101 in base 2: -5
How It Works
Binary."1010" in base 2 is 8 + 2, which is 10.
Octal."777" in base 8 is 7 × 64 + 7 × 8 + 7, which is 511.
Hex ignores case."ff" and "FF" both read as 255, because letters a to f are accepted in either case.
Base 36 uses every letter."zz" is 35 × 36 + 35, which is 1295, and a leading minus still works.
Example 3 — Catch NumberFormatException for Bad Input
Try a mix of good and bad text, and print which ones parseInt rejects.
Wrap the call.tryParse returns the number as text when the parse works, and the exception name when it does not.
Strict rules. A leading space, a decimal point, an empty string, letters, and null are all rejected.
The int range. 2147483647 is Integer.MAX_VALUE and is accepted, but 2147483648 is one too large and fails.
The negative end. -2147483648 is Integer.MIN_VALUE, which has no positive twin, yet parseInt reads it correctly.
Frequently Asked Questions
Call Integer.parseInt(text). It returns a primitive int, or throws NumberFormatException if the text is not a valid whole number.
parseInt does not ignore spaces. Call text.trim() first if the input may have leading or trailing whitespace.
parseInt returns a primitive int, while Integer.valueOf returns an Integer object. Both parse the text with the same rules.
No, "3.5" throws NumberFormatException. Use Double.parseDouble for decimals, or read the whole part yourself.
Pass the base as the second argument: Integer.parseInt("ff", 16) gives 255 and Integer.parseInt("1010", 2) gives 10. Do not include a 0x prefix; decode() handles prefixes.