How to Convert a String to int in Java
Convert a Java string to int with Integer.parseInt and Integer.valueOf, and handle NumberFormatException.
How to Convert a String to int in Java
Turning text like "42" into the number 42 is one of the most common chores in Java — it shows up every time you read command-line arguments, parse a config value, or process form input. The JDK gives you a couple of one-line tools for it in the Integer class, plus a clear failure mode when the text is not a valid number. This chapter shows the idiomatic approaches and how to handle the bad cases.
Integer.parseInt — the primitive int you usually want
Integer.parseInt(String) is the workhorse. It reads the text and returns a primitive int:
int count = Integer.parseInt("42"); // 42
int neg = Integer.parseInt("-7"); // -7A leading + or - is allowed, but nothing else: surrounding spaces, decimal points, underscores, or letters all cause a failure. If you might have whitespace, trim it first with s.trim() (or s.strip() on Java 11+). There is also an overload that takes a radix, so Integer.parseInt("ff", 16) reads hexadecimal and returns 255.
Integer.valueOf — when you need an Integer object
Integer.valueOf(String) does the same parsing but returns an Integer (the boxed wrapper) rather than a primitive int. Reach for it when the value goes straight into a collection like List<Integer> or anywhere an object reference is required. As a bonus, valueOf caches the boxed instances for small values (−128 to 127), so it can be marginally cheaper than parseInt followed by autoboxing.
| Approach | Returns | Use when |
|---|---|---|
Integer.parseInt(s) | primitive int | you want to do arithmetic right away |
Integer.valueOf(s) | Integer object | you need a wrapper for a collection or generics |
Integer.parseInt(s, radix) | primitive int | the text is in base 2, 8, 16, etc. |
Handling invalid input with NumberFormatException
Both methods throw an unchecked NumberFormatException when the string is not a parseable integer — "12x", "3.14", "", or null all fail. Because it is unchecked, the compiler will not force you to catch it, so it is on you to guard inputs you do not control:
String raw = userInput();
try {
int value = Integer.parseInt(raw.trim());
// ... use value
} catch (NumberFormatException e) {
System.out.println("Not a whole number: " + raw);
}When parsing is "expected to fail sometimes" and you do not want exceptions in your control flow, wrap it in a helper that returns an OptionalInt instead of throwing. That turns a try/catch into a clean isPresent() check at the call site.
A worked example: every approach side by side
This program runs each conversion technique and the two failure cases, so you can see exactly what each one returns or prints:
What to take from the run:
Integer.parseInt("42")prints42as a primitiveint, andInteger.valueOf("42")prints the same42— the difference is the type, an object vs. a primitive, not the value.- The radix overload turns
"ff"into255, proving the second argument controls the base; with no radix the default is 10. "12x"is rejected withNumberFormatExceptionwhose message isFor input string: "12x"— the exception names the exact text that failed, which makes it useful in logs.- Passing
nullthrowsNumberFormatException(messageCannot parse null string), notNullPointerException— so a single catch forNumberFormatExceptioncovers both malformed and null input. - The
tryParsehelper trims" 7 "to7and returnsabsentfor"nope", never throwing — that is the pattern to use when invalid input is normal rather than exceptional.
Practice
What happens when you call Integer.parseInt('3.14') in Java?