Convert String to Int in Java

In this tutorial, we will learn how to convert string to primitive int or integer object in Java easily.

There are some situations where we need to convert a number represented as a string into an integer type in Java. It is normally used when we want to perform mathematical operations on the string which contains a number.

For example, whenever we gain data from JTextField or JComboBox, we receive entered data as a string. If entered data is a number in string form, we need to convert the string to an int to perform mathematical operations on it.

Convert String to int in Java

To convert string to an int, we use Integer.parseInt() method from the Integer class. The parseInt() of Integer class is the static method.

So, we do not need to create an object of class to call it. We can call it simply using its class name. The general signature of parseInt() method is as below:

public static int parseInt(String s)

This method accepts a string containing the int representation to be parsed. It returns the integer value. The parseInt() method throws an exception named NumberFormatException if the string does not contain a parsable integer.

Converting String to Int Example Program


Let’s take a simple example program to convert a string to an int in Java.


Example 1:

// Java Program to demonstrate the conversion of String into primitive int  
// using Integer.parseInt() method
package javaConversion;
public class StringToIntConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String text = "50";  
// Convert string into int using Integer.parseInt() method. 
   int num = Integer.parseInt(text);  
// Print value of num.  
   System.out.println(num);
 }
}
Output:
      50

Convert String to Integer Object in Java


There are three ways to convert a string to an integer object. They are as:

  • Convert using Integer.valueOf()
  • Convert using new Integer(String).intValue()
  • Using DecimalFormat

Let’s understand all one by one.

Java String to Integer Example Program: Integer.valueOf()

To convert a string to an integer object, we normally use Integer.valueOf() method of Java Integer class. The general signature of this method is as below:

public static Integer valueOf​(String str)

This method converts a string str containing some int number into an Integer class object and returns that object holding the value of the specified string.

The valueOf() method throws an exception named NumberFormatException if the string cannot be parsed as an integer.

Let’s take a very simple example program to convert String to Integer class object in Java.

Example 2:

// Java Program to demonstrate the conversion of String into integer class object.  
// using Integer.valueOf() method
package javaConversion;
public class StringToIntegerConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String text = "500";  
// Convert string into integer class object using Integer.valueOf() method. 
   Integer num = Integer.valueOf(text);  
// Print value of num.  
   System.out.println(num);
 }
}
Output:
      500

Converting String into Integer Object using new Integer(String).intValue()

Another alternative approach is to create an instance of Integer class and then call intValue() method of Integer class. The intValue() method converts the integer object into primitive int type value. This is called “unboxing” in Java.

Unboxing is a process by which we convert an object into its corresponding primitive data type.


Let’s create a Java program to convert String into an integer object using intValue() method.

Example 3:

// Java Program to demonstrate the conversion of String into integer class object.  
// using Integer(String).intValue() method
package javaConversion;
public class StringToIntegerConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String text = "40";  
// Convert string into integer class object using Integer(String).intValue() method. 
   Integer num = new Integer(text);
   int x = num.intValue();
// Print value of x.  
   System.out.println(x);
 }
}
Output:
      40

Converting String to Integer using DecimalFormat

Java provides a class called DecimalFormat that allows to convert a number to its string representation. This class is present in java.text package. We can also use in other way to parse a string into its numerical representation.


Example 4:

// Java Program to demonstrate the conversion of String into integer object using DecimalFormat class.
package javaConversion;
import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToIntegerConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String text = "400";  
// Create an object of DecimalFormat class.    
   DecimalFormat decimalFormat = new DecimalFormat("#");
   try {
     int num = decimalFormat.parse(text).intValue();
     System.out.println(num);
   } catch (ParseException e) {
     System.out.println(text + " is not a valid number.");
   }
 }
}
Output:
      400

NumberFormatException Case

If we don’t have a number in string literal, invoking Integer.parseInt() or Integer.valueOf() methods can throw an exception named NumberFormatException. Let’s take an example program based on it.

Example 5:

// Java Program to demonstrate the case of NumberFormatException exception.
package javaConversion;
public class StringToIntegerConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "Text";  
// Call parseInt() method of Integer class.     
   int num = Integer.parseInt(str);  
   System.out.println(num);
 }
}
Output:
      Exception in thread "main" java.lang.NumberFormatException: For input string: "Text"
	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.parseInt(Integer.java:786)
	at javaConversion.StringToIntegerConversion.main(StringToIntegerConversion.java:9)

Some More Example Program

Example 6:

package javaConversion;
public class StringToIntConversion {
public static void main(String[] args) 
{
   int decimalEx = Integer.parseInt("30");
   int signedPositiveEx = Integer.parseInt("+30");
   int signedNegativeEx = Integer.parseInt("-30");
   int radixEx = Integer.parseInt("30", 16);
   int stringEx = Integer.parseInt("scien", 29);

// Print the result on console.
   System.out.println(decimalEx);
   System.out.println(signedPositiveEx);
   System.out.println(signedNegativeEx);
   System.out.println(radixEx);
   System.out.println(stringEx);
 }
}
Output:
      30
      30
     -30
      48
      20112103

Example 7:

package javaConversion;
public class StringToIntegerConversion {
public static void main(String[] args) 
{
   int decimalEx = Integer.valueOf("30");
   int signedPositiveEx = Integer.valueOf("+30");
   int signedNegativeEx = Integer.valueOf("-30");
   int radixEx = Integer.valueOf("30", 16);
   int stringEx = Integer.valueOf("scien", 29);

// Print the result on console.
   System.out.println(decimalEx);
   System.out.println(signedPositiveEx);
   System.out.println(signedNegativeEx);
   System.out.println(radixEx);
   System.out.println(stringEx);
 }
}
Output:
      30
      30
     -30
      48
      20112103

In this tutorial, you learned how to convert a string to primitive int or integer class object in Java. I hope that you will have understood and practiced all programs based on string to int conversion.
Thanks for reading!!!