Languages
[Edit]
EN

Java - convert String to int

9 points
Created by:
Payne
654

The best way to convert String to int in java:

String text = "123";
int number = Integer.parseInt(text);

System.out.println(number); // 123

Other short solutions:

// solution 2
Integer number = Integer.valueOf("123"); // 123

// solution 3
Integer number = new Integer("123"); // 123

// solution 4
Integer number = Integer.decode("123"); // 123

// solution 5 - with Guava
Integer number = Ints.tryParse("123"); // 123

In this post we will cover couple of different ways how to convert String to int or Integer.

1. Using Integer.parseInt()

Convert String "123" to primitive int with Integer.parseInt()

public class Example1 {

    public static void main(String[] args) {
        String text = "123";
        int number = Integer.parseInt(text);
        System.out.println(number); // 123
    }
}

Output:

123

2. Using Integer.valueOf()

Convert String "123" to Integer object with Integer.valueOf()

public class Example2 {

    public static void main(String[] args) {
        String text = "123";
        Integer number = Integer.valueOf(text);
        System.out.println(number); // 123
    }
}

Output:

123

3. Using constructor - new Integer(String s)

public class Example3 {

    public static void main(String[] args) {
        String text = "123";
        Integer number = new Integer(text);
        System.out.println(number); // 123
    }
}

Output:

123

4. Using Integer.decode()

public class Example4 {

    public static void main(String[] args) {
        String text = "123";
        Integer number = Integer.decode(text);
        System.out.println(number); // 123
    }
}

Output:

123

5. Using Guava - Ints.tryParse()

import com.google.common.primitives.Ints;

public class Example5 {

    public static void main(String[] args) {
        String text = "123";
        Integer number = Ints.tryParse(text);
        System.out.println(number); // 123
    }
}

Output:

123

6. String to int with sign + or -

Both methods:

  • Integer.parseInt("123")
  • Integer.valueOf("123")

accepts plus sign '+' and minut sign '-'.

Code example:

public class Example6 {

    public static void main(String[] args) {

        // Integer.parseInt() - returns int
        System.out.println(Integer.parseInt("123")); // 123
        System.out.println(Integer.parseInt("+123")); // 123
        System.out.println(Integer.parseInt("-123")); // -123

        // Integer.valueOf() - returns Integer
        System.out.println(Integer.valueOf("123")); // 123
        System.out.println(Integer.valueOf("+123")); // 123
        System.out.println(Integer.valueOf("-123")); // -123
    }
}

Output:

123
123
-123
123
123
-123

7. Example of java.lang.NumberFormatException

public class Example7 {

    public static void main(String[] args) {
        String text = "123a";
        int number = Integer.parseInt(text);
        System.out.println(number);
    }
}

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "123a"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)

Both methods:

  • Integer.parseInt("123")
  • Integer.valueOf("123")

will throw java.lang.NumberFormatException if we try to parse anything else then:
digits or '+', '-' as first character.

As stated in java docs of Integer.parseInt()

The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
Throws: 
NumberFormatException - if the string does not contain a parsable integer

8. ASCII to int - Java atoi implementation

We can convert string to integer by implementing our own atoi() method. ATOI stands for ASCII to integer. Take a look at this atoi java implementation:
ASCII to int - Java implementation

Merged questions

  1. Java - how to convert String to int
  2. Java - cast String to int
  3. How do I change String type to Integer in java?
  4. String to int conversion in java

Reference

  1. Integer.parseInt() - docs
  2. Integer.valueOf() - docs
  3. NumberFormatException - docs
  4. Type conversion - wiki
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join