Convert String to int in Java

In this article, we will take a look at how to convert String to int in Java. Converting String to an int or Integer is a common task and we will take a look at the following options for this conversion.

  1. Java – Convert String to int using Integer.parseInt() method.
  2. Java – Convert String to int using Integer.valueOf() method.

 

1. Convert String to Int using Integer.parseInt()

The parseInt method provide the following 2 options to convert a String to int in Java.

  1. parseInt(String s) – String for converting to int.
  2. parseInt(String s , int radix) –  String with base of the number system.

In the first option, we pass a string as input parameter “345” and parseInt() return the integer value after parsing the input String.Here is a high level signature:

String input ="345";
int output = Integer.parseInt(input);

parseInt() can throws a NumberFormatException if the String can’t be converted.

public class ParseStringToInt {
    public static void main(String[] args) {
        String input ="345";
        String invalid = "javadevjournal";

        /**
         * Convert String to
         * integer with default radix
         */
        int output = Integer.parseInt(input);
        System.out.println(output); //345

        //base 16 conversion using the radix
        int base16 = Integer.parseInt(input, 16);
        System.out.println(base16); //837

        //base 8 conversion using radix
        int base8 = Integer.parseInt(input, 8);
        System.out.println(base8); //229

        //number format exception
        int number1 = Integer.parseInt(invalid);
        System.out.println(number1); //Exception in thread "main" java.lang.NumberFormatException: For input string: "javadevjournal"
    }
}

Integer.ParseInt()  will return an int primitive value.If efficiency is your concern and you only need primitive value, then using Integer.ParseInt()  is the best option.

There are few other important point when you are converting String to integer using Integer.parsseInt() method.

  • Ensure that all characters in the String are digits.
  • The first character of the String can be a “-” sign

Here is a modified version of above example with “-” in the input string value.

public class ParseStringToInt {
    public static void main(String[] args) {
        String input = "-345";
        int number = 445;

        /**
         * Convert String to
         * integer with default radix
         */
        int output = Integer.parseInt(input);
        System.out.println(output); // -345

        /*Let's do some calculation to make
         sure number conversion is working as expected
         */
        number = number + output;
        System.out.println(number); //100

    }
}

 

2.  Convert String to Int using Integer.valueOf()

The Integer.valueOf() method internally use the Integer.parsseInt() method. Here is the method definition from the JDK.

public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}

There are few distinction between the Integer.valueOf() and Integer.parseInt() method. Let’s take a look at them for better understanding.

  1. The valueOf(String) method returns an object of Integer class whereas the parseInt(String) method returns a primitive int value.
  2. We can choose the output as int or Integer class based on our requirement, however keep in mind that there will be Unboxing.
  3. You should use Integer.parseInt() method if you need int value and efficiency is your primary target.

The Integer.valueOf() method provides the following variations

  1. valueOf(String s) – Accepts String and parse it to Integer.
  2. valueOf(int i) – takes int and parse it to Integer.
  3. valueOf(String s, int radix) – String with base of the number system.

Here is a snapshot for the method

String input ="345";
Integer output = Integer.valueOf(input);

Let’s take a look at the complete example to convert String to int

public class ParseStringToInt {
    public static void main(String[] args) {
        String input = "345";
        int number = 445;

        /**
         * Convert String to
         * integer
         */
        Integer output = Integer.valueOf(input);
        System.out.println(output); // 345

        /*
         pass int to convert it to the Integer class
         */
        Integer output1 = Integer.valueOf(number);
        System.out.println(output1); // 445

        /*
         Conversion with a specific base type
         */

        Integer base16 = Integer.valueOf(input, 16);
        System.out.println(base16); //837

        Integer base32 = Integer.valueOf(input, 32);
        System.out.println(base32); //3205
    }
}

The valueOf uses parseInt internally. If efficiency is your concern do not use this method to convert String to int or Integer.

 

3.  NumberFormat Exception

Both Integer.parseInt()  and Integer.valueOf() can throw NumberFormatException if the string cannot be parsed as an integer or int

public class NumberFormatExceptionExample {

    public static void main(String[] args) {
        String invalidNumber = "abc";
        parseIntNFException(invalidNumber);

    }

    public static void parseIntNFException(String number) {
        Integer result = Integer.parseInt(number);
        System.out.println(result);
    }

    public static void valueOfNFException(String number) {
        Integer result = Integer.valueOf(number);
        System.out.println(result);
    }
}

Output

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)
	at NumberFormatExceptionExample.parseIntNFException(NumberFormatExceptionExample.java:13)
	at NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:8)

Note: PraseInt() return a primitive value while valueOf() will always return new Integer object, ideally, if you just want to convert String to an int, it’s always better to avoid creating new Object.

 

4. Convert String to int using Integer.valueOf()

The Integer.decode(String s) method can also be used to convert String to int in Java. This method takes String as input parameter and decode it to Integer. It accepts the following

  1. Decimal.
  2. Hexadecimal
  3. Octal numbers
public class ParseStringToInt {
    public static void main(String[] args) {

        String input1 = "100";
        String input2 = "0x12";
        String input3 = "012";

        /**
         * Convert String to
         * integer using Integer.decode()
         */
        Integer output1 = Integer.decode(input1);
        System.out.println(output1); // 100

        Integer output2 = Integer.decode(input2);
        System.out.println(output2); // 18

        Integer output3 = Integer.decode(input3);
        System.out.println(output3); // 10
    }
}

 

Summary

In this article, we discussed the different options to convert string to int in Java. We saw how to convert it using Integer.valueOf(), Integer.parseInt() and Integer.decode() method.Follow our Java articles to get more in depth knowledge.

1 thought on “Convert String to int in Java”

  1. Hi article is nice,
    In Java there are two methods to convert String to Int, they are.
    Integer.parseInt()
    Integer.valueOf()

Comments are closed.