Java – Convert String to Integer

You can convert a string value to int value in Java using Integer class. Most of the times, your application gets or reads data in the form of string. If you need to extract a number from that string and perform some numeric operations on it, it is necessary that you convert it to an integer or other numeric datatype.

You can typecast or convert a String to Integer in Java in many ways. Some of them are using Integer.parseInt(), Integer.valueOf(), new Integer().

In this tutorial, we shall learn some of the ways of how to convert a string value to a int value with examples.

String to Integer using Integer.parseInt()

Integer.parseInt(str) parses any parsable integer value from string to int value.

In this example, we shall use Integer.parseInt() method and pass a string that can be parsed to a valid int value.

Java Program

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {
		//a string
		String str = "18966354";
		
		//convert string to int
		int n = Integer.parseInt(str);
		
		System.out.print(n);
	}
}

Run the above program and the String is converted to Integer.

18966354

If you do not provide a valid string that is parsable int, Integer.parseInt() throws NumberFormatException.

In the following example program, we shall take a string which does not contain a valid int value.

Some of the scenarios that could throw this error are:

  • If the string contains invalid characters that does not parse to a int value. Like decimal point, alphabets, etc.
  • A number that is out of range for a int value. Any value that is outside the range [-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807] will make parseInt() to throw this error.

Java Program

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {
		//a string
		String str = "5.354"; //"as 9w0", "1as52"
		
		//convert string to int
		int n = Integer.parseInt(str);
		
		System.out.print(n);
	}
}

Run the above program and parseInt() throws NumberFormatException.

Exception in thread "main" java.lang.NumberFormatException: For input string: "5.354"
	at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.base/java.lang.Integer.parseInt(Unknown Source)
	at java.base/java.lang.Integer.parseInt(Unknown Source)
	at StringToInt.main(StringToInt.java:12)

Also, if null is passed to Integer.parseInt(), the function throws NullPointerException.

Java Program

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {
		//a string
		String str = null;
		
		//convert string to int
		int n = Integer.parseInt(str);
		
		System.out.print(n);
	}
}

Run the above program and parseInt() throws NullPointerException.

Exception in thread "main" java.lang.NumberFormatException: null
	at java.base/java.lang.Integer.parseInt(Unknown Source)
	at java.base/java.lang.Integer.parseInt(Unknown Source)
	at StringToInt.main(StringToInt.java:12)

As Integer.parseInt() can throw NullPointerException or NumberFormatException, it is a good practice to surround the function parseInt() with Java Try Catch and handle the exceptions accordingly.

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {
		//a string
		String str = "85612536";
		
		int n = 0;
		
		try {
			//convert string to int
			n = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid int value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

String to Integer using Integer.valueOf()

You can also use Integer.valueOf() function to convert a string to int.

In the following example, we shall use the method valueOf() to get int value from string.

Java Program

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {
		//a string
		String str = "85612536";
		
		int n = 0;
		
		try {
			//convert string to int
			n = Integer.valueOf(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid int value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

Just like Integer.parseInt(), the function Integer.valueOf() also throws NullPointerException if the string argument is null, or a NumberFormatException if the string does not parse to a valid int value.

String to Integer using – new Integer()

Note: new Integer() constructor is depreciated. So, you may get warning when you are following this process. Integer.valueOf() is recommended in the place of new Integer().

You can also use the constructor of Integer class, to convert a string to int.

In the following example, we shall use the constructor of Integer class to convert from string to int.

Java Program

/**
 * Java Program - Convert String to Integer
 */

public class StringToInt {

	public static void main(String[] args) {

		try {
			Integer f = new Integer("52369");
			System.out.print(f);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid int value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
	}
}

Conclusion

In this Java Tutorial, we learned how to Convert a String to Integer value in Java using Integer.parseInt() and Integer.valueOf() methods.