How to convert String to Integer in Java

In Java, we can use Integer.valueOf(String) to convert a String to an Integer object; For unparsable String, it throws NumberFormatException.


  Integer.valueOf("1");      // ok
  Integer.valueOf("+1");     // ok, result = 1
  Integer.valueOf("-1");     // ok, result = -1
  Integer.valueOf("100");    // ok

  Integer.valueOf(" 1");     // NumberFormatException (contains space)
  Integer.valueOf("1 ");     // NumberFormatException (contains space)
  Integer.valueOf("2147483648"); // NumberFormatException (Integer max 2,147,483,647)
  Integer.valueOf("1.1");    // NumberFormatException (. or any symbol is not allowed)
  Integer.valueOf("1-1");    // NumberFormatException (- or any symbol is not allowed)
  Integer.valueOf("");       // NumberFormatException, empty
  Integer.valueOf(" ");      // NumberFormatException, (contains space)
  Integer.valueOf(null);     // NumberFormatException, null

Table of contents

1. Convert String to Integer

Below example uses Integer.valueOf(String) to convert a String "99" to an object Integer.

ConvertStringToInteger.java

package com.mkyong.string;

public class ConvertStringToInteger {

  public static void main(String[] args) {

      String number = "99";

      // String to integer
      Integer result = Integer.valueOf(number);

      // 99
      System.out.println(result);

  }

}

Output

Terminal

  99

2. NumberFormatException

2.1 For unparsable String, the Integer.valueOf(String) throws NumberFormatException.


  String number = "D99";
  Integer result = Integer.valueOf(number);

Output

Terminal

Exception in thread "main" java.lang.NumberFormatException: For input string: "D99"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:668)
	at java.base/java.lang.Integer.valueOf(Integer.java:999)
	at com.mkyong.string.ConvertStringToInteger.main(ConvertStringToInteger.java:10)

2.2 Try and catch the NumberFormatException.

ConvertStringToInteger.java

package com.mkyong.string;

public class ConvertStringToInteger {

    public static void main(String[] args) {

        String number = "D99";
        try {
            Integer result = Integer.valueOf(number);
            System.out.println(result);
        } catch (NumberFormatException e) {
            //do something for the exception.
            System.err.println("Invalid number format : " + number);
        }

    }

}

3. Convert String to Integer (Java 8 Optional)

Below is a Java 8 example of converting a String to an Integer object and returning an Optional<Integer>.

ConvertStringToIntegerJava8.java

package com.mkyong.string;

import java.util.Optional;

public class ConvertStringToIntegerJava8 {

    public static void main(String[] args) {

        String number = "99";

        Optional<Integer> result = convertStringToInteger(number);

        if (result.isPresent()) {
            System.out.println(result.get());
        } else {
            System.err.println("Unable to convert the number : " + number);
        }

    }

    private static Optional<Integer> convertStringToInteger(String input) {

        if (input == null) return Optional.empty();

        // remove spaces
        input = input.trim();

        if (input.isEmpty()) return Optional.empty();

        try {
            return Optional.of(Integer.valueOf(input));
        } catch (NumberFormatException e) {
            return Optional.empty();
        }

    }

}

Output

Terminal

  99

4. Integer.parseInt(String) vs Integer.valueOf(String)

The Integer.parseInt(String) convert a String to primitive type int; The Integer.valueOf(String) convert a String to a Integer object. For unparsable String, both methods throw NumberFormatException.

  • Integer.parseInt() – returns an int.
  • Integer.valueOf() – returns an Integer.

  int result1 = Integer.parseInt("100");

  Integer result2 = Integer.valueOf("100");

5. Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-string

6. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments