Converting one data type to other data types is a common task in the prommer’s life. If we talk about the string to int conversion it can be achieved using two build-in methods i.e., Integer.ParseInt() and Integer.ValueOf(). Usually, we perform the string to int conversion when we have to execute mathematical operations over the strings containing numeric data.

This write-up will explain the below-listed methods for string to int conversion in java:

  • What is the need for string to int conversion?
  • What is Integer.parseInt() and how to use it in java.
  • What is Integer.valueOf() and how to use it in java.

So, let’s begin!

What is the need for string to int conversion?

In Java, anytime we get the input from GUI(TextField/TextArea) then the input is received as a string. If the entered data is a string, that’s fine, but if the entered data is numeric then it can create some serious problems. Because whatever GUI(TextField/TextArea) receives, will be considered string-type data. So, how to tackle such a scenario.

Well! To deal with such issues java provides some built-in functions like Integer.parseInt() and Integer.ValueOf().

What is Integer.parseInt() and how to use it in java

It is a predefined static method used to convert a string to an integer.

Example1


For the clarity of concept, consider the below-given code block:

public class StringtoInt {


    public static void main(String args[]) {


        String value = “572”;


        int converted = Integer.parseInt(value);


        System.out.println(“Converted value: “ converted);


    }

}

The Integer.parseInt() method will produce the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/How-to-convert-string-1.png" data-lazy- height="274" src="data:image/svg xml,” width=”695″>

The output shows an integer value.

Example2


The below code block will provide you more clarity:

public class StringtoInt {


    public static void main(String args[]) {


        String value = “72”;


        System.out.println(“Result Before Conversion: “ (value value));


        int converted = Integer.parseInt(value);


        System.out.println(“Converted value: “ converted);


        System.out.println(“Result after Conversion: “ (converted converted));


    }

}

In this example, firstly, we applied the arithmetic addition operator on the original value, and afterward, we performed the same functionality on the converted values. Consequently, we will get the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/How-to-convert-string-2.png" data-lazy- height="321" src="data:image/svg xml,” width=”692″>

The output shows that Integer.parseInt() converted the given string into integer data type successfully.

What is Integer.valueOf() and how to use it in java

Java provides another useful method to convert a string to an integer value named Integer.valueOf().

Example3


In this example, we will utilize the Integer.valueOf() method to convert the string data into integer format in java:

public class StringtoInt {


  public static void main(String args[]) {


    String value = “12”;


    System.out.println(“Result Before Conversion: “ (value value));


    int converted = Integer.valueOf(value);


    System.out.println(“Result after Conversion: “ (converted converted));


    }

}

The Integer.valueOf() method will generate the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/How-to-convert-string-3.png" data-lazy- height="297" src="data:image/svg xml,” width=”696″>

The output shows that the valueOf() method does exactly what we were looking for.

Example 4


Consider the below code snippet to understand how valueOf() deal with invalid(non-numeric) strings:

public class StringtoInt {


  public static void main(String args[]) {


    String value = “Java512”;


    int converted = Integer.valueOf(value);


    System.out.println(“Result: “ ( converted));


    }

}

If we pass a non-numeric string to the valueOf() method it will throw a NumberFormatException:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/How-to-convert-string-4.png" data-lazy- height="418" src="data:image/svg xml,” width=”716″>

It’s better to surround your code with try catch to avoid such type of exception.

Conclusion

In java, string to int conversion can be achieved using two build-in methods i.e., Integer.ParseInt() and Integer.ValueOf(). These methods take numeric strings as input and convert them into integer data types. If we pass an invalid/non-numeric string to these methods, then a Number Format Exception will occur. This write-up explained different methods to convert a string to int in java.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg628b595a52b0f.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.