How to convert string to int in Java

Sometimes we require to perform a mathematical operation on data received as a string. At that time we have to convert the string value into an integer, and then we can perform operations on it.

Note:

The data received from the text field would always be a string form.
Java provides the methods for converting the string into an integer.

How to convert JAVA string to int?

Integer.parseInt() is the function used for converting the string into an integer.

This method is the static method of the class Integer. The signature of the parseInt function is as below:

public static int parseInt(String s)

Example 1

Suppose we get the number as a text field of the form. Then we can convert that string into int as follows:

int x = Interger.parseInt(“15”);

The above code converts the string 15 into an integer and then assigns it to integer variable x.

Example 2

// Java program for converting the string into an integer using parseInt() function

public class stringtoint{
Public static void main(String args[]){
String s = "20"; // declaration
system.out.println(s); // string value
int x = Integer.parseInt(s);
// convertion of string into integer
system.out.println(x); // int value

}

}

Output:

20
20

Explanation:

  • First, we declare the string s and assign a value.
  • Next, we print the string value using println() function of JAVA.
  • Then we convert that string into an integer using parseInt() function and assign it to variable x.
  • Then after we print the integer value using println() function of JAVA.

Note: In the above example, we print the value of the variable. So we cannot distinguish the changes in the result.

Let’s consider the following example, which creates a significant difference between the result of integer and string type values.

Example 3

In this example, we use JAVA concatenation for understanding the use of parseInt() function in a better way.

// Java program for converting the string into an integer using parseInt() function with concatenation

public class stringtoint{
Public static void main(String args[]){
String s = "20"; // declaration
system.out.println(s + 19);
// result: 2019 because of the concatination of two strings
int x = Integer.parseInt(s);
// convertion of string into integer
system.out.println(x + 19);
// result: 39 because of the addition of two numbers

}

}

Output:

2019
39

Explanation:

  • First, we declare the string s and assign a value.
  • Next, we print the string value with the concatenation of 19 using println() function of JAVA. It results in 2019 because here 20 is considered as a string.
  • Then we convert that string into an integer using parseInt() function and assign it to variable x.
  • Then after we print the integer value with concatenation of 19 results in addition of 20 + 19 = 39 using println() function of JAVA.

JAVA language also provides the Integer.valueOf() method for converting the string into an integer.

Note: In this method, we can assign value only to an object of class Integer, and in parseInt() method we assign value to a variable of type int.

Example 4

// Java program for converting the string into an integer using Integer.valueOf() function

public class stringtoint{
Public static void main(String args[]){
String s = "20"; // declaration
system.out.println(s); // string value
Integer x = Integer.valueOf(s);
// convertion of string into integer
system.out.println(x); // int value

}

}

Output:

20
20

Explanation:

  • First, we declare the string s and assign a value.
  • Next, we print the string value using println() function of JAVA.
  • Then we convert that string into an integer using valueOf() function and assign it to an object of class Integer.
  • Then after we print the integer value using println() function of JAVA.

Conclusion

This all about the conversion of JAVA string into an integer. This method is helpful when we need to take some number form string and to do some operations on that number.

Leave a Reply

Your email address will not be published. Required fields are marked *