Categories
integer java string type-conversion

How do I convert a String to an int in Java?

3388

How can I convert a String to an int?

"1234"  →  1234

3

  • 4

    By the way, be aware that if the string is null, the call: int i = Integer.parseInt(null); throws NumberFormatException, not NullPointerException.

    Jun 20, 2018 at 21:26

  • 9

    I’m a little surprised that this question should be so highly rated when an important part of the specification is missing: what should happen if the string DOESN’T contain only digits? For example, many of the answers will happily parse “-1” and return -1, but we don’t know if that’s acceptable.

    Aug 6, 2019 at 10:29

  • 1

    The question needs to clarify the radix. Is it 10, 16, 8 ? what ? Maybe base 7

    Feb 9 at 6:20

4420

String myString = "1234";
int foo = Integer.parseInt(myString);

If you look at the Java documentation you’ll notice the “catch” is that this function can throw a NumberFormatException, which you can handle:

int foo;
try {
   foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
   foo = 0;
}

(This treatment defaults a malformed number to 0, but you can do something else if you like.)

Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8’s Optional, makes for a powerful and concise way to convert a string into an int:

import com.google.common.primitives.Ints;

int foo = Optional.ofNullable(myString)
 .map(Ints::tryParse)
 .orElse(0)

2

  • 41

    In addition to catching a NumberFormatException, the user should also be careful about the length of the strings they’re passing in; if they’re long enough to overflow an integer, they might want to consider using Long::parseLong instead.

    – Allison

    Jan 17, 2018 at 9:37

  • It works perfectly. Thank you!

    Jan 13 at 5:50

756

For example, here are two ways:

Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);

There is a slight difference between these methods:

  • valueOf returns a new or cached instance of java.lang.Integer
  • parseInt returns primitive int.

The same is for all cases: Short.valueOf/parseShort, Long.valueOf/parseLong, etc.

2

  • 85

    For the differences between the two methods, see this question

    May 19, 2013 at 8:38

  • 22

    valueOf method is just return valueOf(parseInt(string));

    Oct 28, 2014 at 8:55

265

Well, a very important point to consider is that the Integer parser throws NumberFormatException as stated in Javadoc.

int foo;
String StringThatCouldBeANumberOrNot = "26263Hello"; //will throw exception
String StringThatCouldBeANumberOrNot2 = "26263"; //will not throw exception
try {
      foo = Integer.parseInt(StringThatCouldBeANumberOrNot);
} catch (NumberFormatException e) {
      //Will Throw exception!
      //do something! anything to handle the exception.
}

try {
      foo = Integer.parseInt(StringThatCouldBeANumberOrNot2);
} catch (NumberFormatException e) {
      //No problem this time, but still it is good practice to care about exceptions.
      //Never trust user input :)
      //Do something! Anything to handle the exception.
}

It is important to handle this exception when trying to get integer values from split arguments or dynamically parsing something.

2