W3docs

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");   // -7

A 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.

ApproachReturnsUse when
Integer.parseInt(s)primitive intyou want to do arithmetic right away
Integer.valueOf(s)Integer objectyou need a wrapper for a collection or generics
Integer.parseInt(s, radix)primitive intthe 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:

java— editable, runs on the server

What to take from the run:

  • Integer.parseInt("42") prints 42 as a primitive int, and Integer.valueOf("42") prints the same 42 — the difference is the type, an object vs. a primitive, not the value.
  • The radix overload turns "ff" into 255, proving the second argument controls the base; with no radix the default is 10.
  • "12x" is rejected with NumberFormatException whose message is For input string: "12x" — the exception names the exact text that failed, which makes it useful in logs.
  • Passing null throws NumberFormatException (message Cannot parse null string), not NullPointerException — so a single catch for NumberFormatException covers both malformed and null input.
  • The tryParse helper trims " 7 " to 7 and returns absent for "nope", never throwing — that is the pattern to use when invalid input is normal rather than exceptional.

Practice

Practice

What happens when you call Integer.parseInt('3.14') in Java?