Convert String to int in Java (Integer.parseInt)
Converting a String to an int is one of the most frequent operations in Java β parsing command-line arguments, reading configuration, processing user input. The canonical tool is Integer.parseInt(), with a few useful variations.
The simplest form
int n = Integer.parseInt("42");
System.out.println(n); // 42
If the string is not a valid integer, parseInt throws NumberFormatException.
Integer.parseInt vs Integer.valueOf
| Method | Returns | When to use |
|---|---|---|
Integer.parseInt(s) | primitive int | Most of the time |
Integer.valueOf(s) | wrapper Integer | When you need a boxed Integer, or to benefit from the cache (-128 to 127) |
int primitive = Integer.parseInt("42");
Integer object = Integer.valueOf("42"); // boxes, may reuse cached instance
Handling exceptions
String input = "abc";
try {
int n = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.err.println("Not a valid integer: " + input);
}
The exception message tells you what went wrong: For input string: "abc".
Whitespace is not tolerated
Integer.parseInt(" 42 "); // β NumberFormatException
Trim before parsing if your input may have whitespace:
int n = Integer.parseInt(input.trim());
Parsing with a different radix (base)
The second argument sets the base (2 to 36):
int bin = Integer.parseInt("101010", 2); // 42
int hex = Integer.parseInt("2A", 16); // 42
int oct = Integer.parseInt("52", 8); // 42
int b36 = Integer.parseInt("16", 36); // 42
Useful when parsing hex codes, binary masks, or data from legacy systems.
Signed numbers
Negative and explicitly positive numbers are supported:
Integer.parseInt("-42"); // -42
Integer.parseInt("+42"); // 42
Safe parsing without exceptions
For input that might be invalid, return an OptionalInt instead of throwing:
import java.util.OptionalInt;
public static OptionalInt tryParseInt(String s) {
if (s == null) return OptionalInt.empty();
try {
return OptionalInt.of(Integer.parseInt(s.trim()));
} catch (NumberFormatException e) {
return OptionalInt.empty();
}
}
// Usage
int port = tryParseInt(System.getenv("PORT")).orElse(8080);
Parsing unsigned values
For values above Integer.MAX_VALUE (up to 2^32 - 1), use:
int unsigned = Integer.parseUnsignedInt("4294967295"); // -1 (all bits set)
The bits are stored correctly but the int is still signed β use Integer.toUnsignedLong(unsigned) to read the true value.
Large numbers
If the value could exceed Integer.MAX_VALUE (2 147 483 647), use long or BigInteger:
long big = Long.parseLong("9999999999");
import java.math.BigInteger;
BigInteger huge = new BigInteger("99999999999999999999999");
Locale-aware parsing
Integer.parseInt does not accept thousand separators. For locale-formatted numbers ("1,234" or "1 234"), use NumberFormat:
import java.text.NumberFormat;
import java.util.Locale;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
int n = nf.parse("1,234").intValue(); // 1234
Scanner for interactive input
If you're reading from System.in, Scanner.nextInt() handles parsing and whitespace:
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
On invalid input, nextInt throws InputMismatchException.
Common mistakes
- Not trimming whitespace β fails on trailing newlines or spaces from user input.
- Expecting
"3.14"to parse β it won't. UseDouble.parseDoublefor decimals. - Leading zeros β
"007"parses as 7 (not octal). ButInteger.parseInt("010", 8)= 8. - Forgetting overflow β
"9999999999"overflowsint. Uselong. - Null input β throws
NumberFormatException, not NPE, but still crashes.
Quick cheat sheet
// Simple known-good input
int a = Integer.parseInt("42");
// User input β trim and handle errors
int b = Integer.parseInt(userInput.trim());
// Safe optional version
OptionalInt c = tryParseInt(userInput);
// Hex, binary, octal
int d = Integer.parseInt("FF", 16);
// Locale with separators
int e = NumberFormat.getInstance(Locale.US).parse("1,234").intValue();
// Potentially huge
long f = Long.parseLong("9999999999");
Pick the right tool for your input and you'll handle every real-world parsing case cleanly.