Java String to Int – Definitive Guide

To convert Java String to Int is one of the commonly used operation in the Java programs. There are 8 methods available to perform this conversion.

You can convert Java String to Int using Integer.parseInt() method or Integer.valueOf() method.

In this tutorial, you’ll learn

  • Java APIs available to convert String to int or Integer
  • External Libraries available to convert String to Int
  • When to use the different methods
  • Exceptions to be handled

The recommended method to convert String to int is using the Java API itself as given below.

try {
    final int result = Integer.parseInt(numAsString);

    System.out.println("String converted to number using Integer.parseInt : " + result);

    } catch (final NumberFormatException e) {

        System.err.println("Unable to convert String  " + numAsString
                    + " to int. A NumberFormatException Occured, Ensure the string contains only valid numbers " + e);
    }

Read the below detailed methods to understand how to convert Java String to int using various methods available and when its appropriate to use each method.

Using Integer.parseInt()

You can easily convert String to Int in java using the Integer.parseInt() method available by default in Java.

There are two overloaded methods available in Integer.parseInt() as explained below

Integer.parseInt(String Str)

Integer.ParseInt(String str) accepts only a String which needs to be converted to int.

When to Use

Use this method when you want to convert a String which has only numbers and a default radix shall be used to convert.

Points to Note

The leading zeros will be ignored when converting String to int using the parseInt() method.

If you want to preserve the leading zeros, then do not use this method.

Snippet

The below is the code snippet to convert the string to int.

final int result = Integer.parseInt(numAsString);

Returns

A primitive int type representing the String.

Exceptions to Handle

The Integer.parseInt() throws the NumberFormatException in the following cases.

  • If the String is null or Empty.
  • If the String is not a parse-able integer. For e.g., it contains any characters other than numbers. Except the – sign which can be used to denote a negative number.

Using Integer.parseInt(String) to Convert With Leading Zeros

If you want to convert String to int with leading zeros, then use the below code snippet.

%05d in the string format method specifies, 5 digit leading zeros should be preserved.

public static void main(String args[]){

      String str="0000077";
      
      str = String.format("%05d", Integer.parseInt(str));

      System.out.println("Output String: "+str);

  }

Integer.parseInt(String Str, Int Radix)

Integer.parseInt(String str, int radix) accepts a String to be converted to int and a radix to convert the String to a specific radix.

When to Use

You can use this method if you want to convert a String to int with specified Radix.

Code snippet

The below code is used to convert the String to int in the specified Radix.

final int result = Integer.parseInt(numAsString,10);

Returns

A primitive int type representing the String.

Exceptions to Handle

The Integer.parseInt() throws the NumberFormatException in the following cases.

  • If the String is null or Empty.
  • If the String is not a parse-able integer. For e.g., it contains any characters other than numbers. Except the – sign which can be used to denote a negative number.

Example Program

Below example shows how to use the Integer.parseInt() with NumberFormatException.

private static void convertUsingParseInt() {

        final String numAsString = "-500";

        try {

            final int result = Integer.parseInt(numAsString);

            System.out.println("String converted to number using Integer.parseInt : " + result);

        } catch (final NumberFormatException e) {

            System.err.println("Unable to convert String  " + numAsString
                    + " to int. A NumberFormatException Occured, Ensure the string contains only valid numbers " + e);

        }
    }

Using Integer.valueOf()

You can easily convert String to Integer in Java using the Integer.valueof() method. This can be used to convert Java string to Integer without parseInt() method.

There are two overloaded methods available as explained below.

Using Integer.valueOf(String S)

Integer.valueOf(String s) accepts only a String object to be converted to Integer.

When to Use

You can use this method, if you want to convert String to an Integer Object rather than the primitive int.

Returns

Returns Integer object representing the String.

If the Integer object is already available in the cache, then it returns the cached object.

Else it returns the new Integer object.

Code snippet

Below Snippet is used to convert to the String to Integer using valueOf().

final int result = Integer.valueOf(numAsString);

Exceptions to Handle

NumberFormatException will be thrown by Integer.valueOf(String s) in following scenarios.

  • If the String is null or empty
  • If the String contains any characters other than integers. Except the ‘-‘ sign which can be used to denote the negative number.

Example Program

The below example shows how to convert String to Integer using the valueOf() method.

/**
     * 
     */
    private static void convertUsingValueOf() {

        final String numAsString = "500";

        try {

            final int result = Integer.valueOf(numAsString);

            System.out.println("String converted to number using Integer.valueOf : " + result);

        } catch (final NumberFormatException e) {

            System.err.println("Unable to convert String  " + numAsString
                    + " to int. A NumberFormatException Occured, Ensure the string contains only valid numbers " + e);

        }
    }

Using Apache Commons Lang3

Apache Commons Lang is a provides lot of helper methods for Java.lang API, especially String manipulations, Number conversions, objects reflections etc.

Latest Commons Lang3 Version: 3.11

You can download the binaries of the Commons Lang from this link.

Here is the download home page to get the other format of the binaries.

Pom File

To add commons lang as a dependency in Maven project, add the below code to your POM file.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Gradle Location

implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'

You can use the toInt() available in the NumberUtils class of Commons lang API.

Now, you’ll see how to use the two different NumberUtils.toInt() to perform the convert operation.

NumberUtils.toInt(String Str)

NumberUtils.toInt(String str) accepts only the String object that will be converted to int.

This can be used to convert Java string to int without exception and it is null safe.

When to Use

Use if you do not want to handle any exception explicitly.

Returns

int represented by the String object.

0 – if the conversion fails due to any error or exception.

Code snippet

Use the below snippet to convert to String to int using toInt().

final int result = NumberUtils.toInt(numAsString);

Exceptions to Handle

No exceptions to Handle as it returns 0 in case of exceptions.

Example Program

The below example shows how to convert String to Integer using the toInt() method.

/**
     * 
     */
    private static void convertUsingValueOf() {

        final String numAsString = "500";

        final int result = NumberUtils.toInt(numAsString);

        System.out.println("String converted to number using NumberUtils.toInt() : " + result);

    }

Using NumberUtils.toInt(String Str, Int defaultValue)

NumberUtils.toInt(String str, int defaultValue) accepts the String object that will be converted to int and a default value which should be returned in case if the conversion fails.

This can be used to convert Java string to int without exception and it is null safe.

When to Use

Use if you want to use any default value in case of failed conversion.

Returns

int represented by the string object, if the conversion is successful.

Default value, if the conversion fails due to any Exception.

Code snippet

Use the below snippet to convert to the String to int using toInt().

final int result = NumberUtils.toInt(numAsString,1);

Exceptions to Handle

No exceptions to Handle as it returns the defaultValue in case of exceptions.

Example Program

The below example shows how to convert String to int using the toInt() method.

/**
     * 
     */
    private static void convertUsingtoInt() {

        final String numAsString = "500";

        final int result = NumberUtils.toInt(numAsString, 1);

        System.out.println("String converted to number using NumberUtils.toInt() : " + result);

    }

Using isDigit() and parseInt()

In this section, you’ll use the user defined function isDigit() to check if the given String is a proper number which can be converted to int or if it contains any characters which cannot be converted to int or Integer object.

This can also be used to convert Java string to int without exception.

If its a proper digit, then you can use parseInt() method available in the Integer class to convert String to Int primitive type and it is null safe.

When to Use

Use if

  • You want to check if the string is a proper number before converting.
  • You do not want to handle NumberFormatException in your program.

Example Program

In the below example class name StringtoIntExamples, isDigit() method is used to check if its a convertible String and then convert to int.

public class StringtoIntExamples {

    public static void main(final String[] args) {

        convertUsingParseInt();

    }

    /**
     * @param input
     * @return
     */
    public static boolean isDigit(final String inputString) {

        // input is null or an Empty String, return false.

        if (inputString == null || inputString.isEmpty())

            return false;

        if (inputString.startsWith("-"))

            // negative number in string, cut the first char
             return inputString.substring(1).matches("[0-9]*");

        else

            // positive number return true for converting to int
            return inputString.matches("[0-9]*");


    }

    /**
     * Method to convert Java String to int using Integer parseInt.
     */
    private static void convertUsingParseInt() {

        String number = "-100"; 

        if (isDigit(number)) {      

            System.out.println(Integer.parseInt(number));
 
        } 
        else {
 
            System.out.println("Please provide a String containing only numbers");
 
        }
    }

Using Java 8 Optional and Streams

In this section, you’ll learn how to use Java8 Optional() and Streams to convert String to int.

Optional is a container object in java which may or may not contain values. This can be used when you want to avoid null check of any objects.

Streams are Classes which supports functional-style operations on streams of elements, such as map-reduce transformations on collections.

This is null safe method.

Snippet

final Optional<Integer> result = Optional.ofNullable(number).filter(StringtoIntExamples::isDigit)
                .map(Integer::parseInt);

where

  • Optional.ofNullable(number) – Checks if the String is null or not
  • filter(StringtoIntExamples::isDigit) – Checks if the String is a proper digit. Uses the isDigit() user defined method available in the StringtoIntExamples class.
  • map(Integer::parseInt) – Converts the String to int using the Integer.parseInt() using Streams.

Example

If the String is not null, and a proper number means, then the resultant Optional object will return true when checked with isPresent().

If true then, return the number. Else display appropriate message to user.

/**
     * Method to convert Java String to int using Java 8 optional.
     */
    private static void convertUsingOptional() {

        final String number = "10";

        final Optional<Integer> result = Optional.ofNullable(number).filter(StringtoIntExamples::isDigit)
                .map(Integer::parseInt);

        if (result.isPresent())
            System.out.println(result.get());
        else
            System.out.println("Please provide a valid digit [0-9]");

    }

This is how you can use Java8 Optional and Stream to convert String to Integer.

Using Google’s Guava Library

Google has a java core library called Guava with multiple utility methods and additional collection types.

You can use the tryParse() method available Ints class of Guava library to convert String to int without exception in case of problems.

Latest Google Guava Version: 30.1.1

You can download the binaries of the Google Guava from this link.

Here is the download home page to get the other format of the binaries.

Pom File

To add commons lang as a dependency in Maven project, add the below code to your POM file.

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>30.1.1-jre</version>
  <!-- or, for Android: -->
  <version>30.1.1-android</version>
</dependency>

Gradle Location

dependencies {
  // Pick one:

  // 1. Use Guava in your implementation only:
  implementation("com.google.guava:guava:30.1.1-jre")

  // 2. Use Guava types in your public API:
  api("com.google.guava:guava:30.1.1-jre")

  // 3. Android - Use Guava in your implementation only:
  implementation("com.google.guava:guava:30.1.1-android")

  // 4. Android - Use Guava types in your public API:
  api("com.google.guava:guava:30.1.1-android")
}

Using tryParse(String Str)

tryParse(String str) parses the string into a signed decimal Integer value. This method accepts only ascii digits and the + ascii sign is rejected by default.

When to Use

You can use this method, if you want to avoid validation if the string is a valid number.

It is also null safe.

Returns

  • Number representing the String

Code snippet

Integer result = Ints.tryParse(givenString);

Exceptions to Handle

No exception needs to be handled explicitly. It returns null if there is a problem during conversion.

Example Program

/**
* 
*/
private int void convertUsingtoInt(String numAsString) {

    final int result = Ints.tryParse(numAsString);

    System.out.println("String converted to number using NumberUtils.toInt() : " + result);

    return result;
}

Using Integer.decode()

Integer.decode() works similar to Integer.valueOf() but accepts different representations such as decimal, hexadecimal and octal numbers.

When to Use

Use when you want to convert a String with decimal, hexadecimal or octal number representations.

Returns

  • int represented by the String

Code snippet

int result = Integer.decode("500");

Exceptions to Handle

NumberFormatException will be thrown by Integer.decode(String s) in following scenarios.

  • If the String is null or empty
  • If the String contains any characters other than integers. Except the ‘-‘ sign which can be used to denote the negative number.

Conclusion

String to int conversion is one of the commonly used operation when handling lot of user inputs. With Java evolving continuously, there are number of methods available to convert String to int or String to Integer object.

In this tutorial, You’ve learnt all the methods available to convert String to int in Java.

Feel free to comment if you have any questions or feedback.

Leave a Comment

Share via
Copy link
Powered by Social Snap