Convert String to int in Java

Learn to convert a Java String to int value. Note that string can contain a normal decimal value or a different value in other bases or radix.

1. Using Integer.parseInt()

1.1. Syntax

The Integer.parseInt() parses a String to a signed int value. This method is an overloaded method with the following arguments:

  • string: the string to be parsed.
  • radix: radix to be used while parsing.
  • beginIndex: beginning index, inclusive.
  • endIndex: ending index, exclusive.
public static int parseInt(string) throws NumberFormatException { ... }
public static int parseInt(string, int radix) throws NumberFormatException { ... }
parseInt(string, int beginIndex, int endIndex, int radix) throws NumberFormatException { ... }

1.2. Example

In the following Java program, we are parsing the string “1001” using all three above-mentioned methods and verifying that output.

Assertions.assertEquals(1001, Integer.parseInt("1001"));
Assertions.assertEquals(513, Integer.parseInt("1001", 8));
Assertions.assertEquals(1001, Integer.parseInt("amount=1001", 7, 11, 10));

1.3. Throws NumberFormatException

All three methods throw NumberFormatException if:

  • the argument string is null.
  • the argument string length is zero i.e. empty string.
  • the argument string is not a parsable integer in the specified radix. The default radix is 10.
Assertions.assertThrows(NumberFormatException.class, ()->{
  Integer.parseInt(null);
});

Assertions.assertThrows(NumberFormatException.class, ()->{
    Integer.parseInt("abc");
});

2. Using Integer.valueOf()

The Integer.valueOf() is very similar to parseInt() method – with only one difference that return type is Integer type instead of primitive int. In fact, if we look at the source code of valueOf() method, it internally calls parseInt() method.

It is also overloaded in two forms. Notice the method return type is Integer.

public static Integer valueOf(String s) throws NumberFormatException {...}
public static Integer valueOf(String s, int radix) throws NumberFormatException {...}

Both methods throw the NumberFormatException in all cases, similar to parseInt().

Assertions.assertEquals(1001, Integer.valueOf("1001"));
Assertions.assertEquals(513, Integer.valueOf("1001", 8));

Assertions.assertThrows(NumberFormatException.class, ()->{
  Integer.valueOf("abc");
});

3. Using Integer.decode()

The Integer.decode() is another useful method for String to int conversion but only for decimal, hexadecimal and octal numbers. Note that:

  • Decimal numbers should start with optional plus/minus sign i.e. +100, -2022, 334, 404.
  • Octal numbers should start with an optional plus/minus sign and then the suffix ‘0’ (Zero). For example, +o100, -o2022, o334, 0404.
    • Hex numbers should start with an optional plus/minus sign and then the suffix ‘0x’ or ‘0X’ (Zero and X). For example, +0x100, -0x2022, 0x334, 0x404.
    public static Integer decode(String s) throws NumberFormatException

    Let us see an example to understand it better.

    Assertions.assertEquals(100, Integer.decode("+100"));
    Assertions.assertEquals(64, Integer.decode("+0100"));
    Assertions.assertEquals(256, Integer.decode("+0x100"));

    4. Conclusion

    In this quick Java tutorial, we learned to parse a string to int or Integer type. We also learned to parse the numbers in decimal, octal and hexadecimal formats.

    Happy Learning !!

    Sourcecode on Github

    Comments

    Subscribe
    Notify of
    guest
    0 Comments
    Inline Feedbacks
    View all comments

    About Us

    HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

    It also shares the best practices, algorithms & solutions and frequently asked interview questions.

    Our Blogs

    REST API Tutorial

    Dark Mode

    Dark Mode