In this Java tutorial, you will learn how to convert a String to an int using Integer.parseInt() and to an Integer object using Integer.valueOf(). You will also learn how invalid input, null values, whitespace, number ranges, and different numeric bases affect String-to-integer conversion.

Java – Convert String to Integer

Java programs often receive numeric data as text, for example from user input, files, command-line arguments, form fields, or external data sources. Before performing integer arithmetic on that text, you can convert the String representation of a whole number to an int or Integer.

The two methods normally used for this conversion are Integer.parseInt() and Integer.valueOf(). Integer.parseInt() returns a primitive int, while Integer.valueOf() returns an Integer object.

ConversionMethodReturn type
String to primitive integerInteger.parseInt(str)int
String to Integer objectInteger.valueOf(str)Integer

The older Integer(String) constructor also appears later in this tutorial because it exists in the original example, but it is deprecated and should not be used in new code.

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

Integer.parseInt(str) parses a String containing a valid decimal integer and returns the corresponding primitive int value.

</>
Copy
int number = Integer.parseInt(stringValue);

For example, the String "18966354" contains only decimal digits and represents a value within the Java int range, so it can be parsed successfully.

Java Program

</>
Copy
/**
 * 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

The variable n in this example is specifically an int. The displayed digits look the same as the original String, but the value can now be used directly in integer arithmetic.

2. NumberFormatException when a Java String is not a valid int

If the String cannot be parsed as a valid int, Integer.parseInt() throws NumberFormatException.

Common causes include:

  • The String contains decimal notation such as "5.354". parseInt() parses integers, not floating-point values.
  • The String contains letters or other characters that are not valid for the selected radix.
  • The String is empty.
  • The String contains only whitespace.
  • The String includes leading or trailing whitespace, such as " 42 ", unless that whitespace is removed before parsing.
  • The numeric value is outside the Java int range of -2147483648 through 2147483647.
  • The reference passed to parseInt() is null.

In the following example, "5.354" is not a valid representation of an int because it contains a decimal point.

Java Program

</>
Copy
/**
 * 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)

If your input is intended to represent a decimal number rather than an integer, use the parsing method for the required numeric type, such as Double.parseDouble() or Float.parseFloat(), instead of removing the decimal point.

3. What happens when Integer.parseInt() receives null?

Passing a null String reference to Integer.parseInt() results in a NumberFormatException. It does not produce a valid integer value.

Java Program

</>
Copy
/**
 * 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);
	}
}

The existing example produces the following exception when the null value is parsed.

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)

This means code that parses input should account for invalid and null values before relying on the converted result.

4. Handle invalid String-to-int conversion with try-catch

When input may not contain a valid integer, you can use Java Try Catch to handle NumberFormatException instead of allowing the program to terminate at the parsing statement.

The following existing example demonstrates exception handling around the conversion.

</>
Copy
/**
 * 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);
	}
}

For Integer.parseInt(), invalid numeric text and a null String are handled as NumberFormatException. A separate NullPointerException catch is therefore not required specifically for parseInt().

</>
Copy
String str = null;

try {
    int number = Integer.parseInt(str);
    System.out.println(number);
} catch (NumberFormatException e) {
    System.out.println("Input is not a valid int.");
}

5. Convert String to Integer using Integer.valueOf()

Integer.valueOf(String) parses the String and returns an Integer object rather than a primitive int.

</>
Copy
Integer number = Integer.valueOf(stringValue);

If you assign that result to an int, Java can automatically unbox the returned Integer object to a primitive value.

In the following existing example, the result of Integer.valueOf(str) is assigned to an int, so unboxing occurs automatically.

Java Program

</>
Copy
/**
 * 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);
	}
}

Like Integer.parseInt(), Integer.valueOf(String) throws NumberFormatException when the supplied String cannot be interpreted as an int, including when the String reference is null.

6. Integer.parseInt() vs Integer.valueOf() in Java

The main difference between these methods is the return type. Choose the method according to the type your program needs.

MethodResultTypical use
Integer.parseInt("42")primitive intUse when you need an integer value for calculations or primitive variables.
Integer.valueOf("42")Integer objectUse when an object is required, such as in APIs or generic collections that use Integer.

If your target variable is an int, Integer.parseInt() expresses that intent directly. If your target is an Integer, use Integer.valueOf().

7. Convert a String with spaces to int in Java

Integer.parseInt() does not automatically ignore surrounding whitespace. If input may contain leading or trailing spaces, remove them before parsing. The trim() method handles many common whitespace inputs, while strip() provides Unicode-aware stripping in modern Java.

</>
Copy
public class StringToInt {
    public static void main(String[] args) {
        String str = "  125  ";
        int number = Integer.parseInt(str.trim());

        System.out.println(number);
    }
}

Output

125

Do not remove arbitrary non-numeric characters simply to force parsing to succeed. Validate or normalize input according to what the application actually accepts.

8. Convert binary, hexadecimal, or another radix String to int

Integer.parseInt() also has an overload that accepts a radix. The radix specifies the numeric base used to interpret the String.

</>
Copy
int number = Integer.parseInt(stringValue, radix);

For example, "1010" represents decimal 10 when interpreted as a base-2 number.

</>
Copy
public class StringToInt {
    public static void main(String[] args) {
        int binary = Integer.parseInt("1010", 2);
        int hexadecimal = Integer.parseInt("FF", 16);

        System.out.println(binary);
        System.out.println(hexadecimal);
    }
}

Output

10
255

The characters in the String must be valid digits for the specified radix, and the resulting value must still fit within the Java int range.

9. Convert String to Integer using the deprecated Integer() constructor

Note: the Integer(String) constructor is deprecated. New code should use Integer.valueOf() when an Integer object is needed, or Integer.parseInt() when a primitive int is needed.

The following older approach uses the constructor of the Integer class. It is retained here to explain code you may encounter in older Java programs.

Java Program

</>
Copy
/**
 * 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.");
		}
		
	}
}

For current code, the equivalent object conversion is simply Integer.valueOf("52369").

10. String values that Integer.parseInt() can and cannot parse

StringResult with parseInt()Reason
"123"123Valid decimal integer
"-123"-123A leading minus sign is valid
"+123"123A leading plus sign is valid
"5.354"NumberFormatExceptionContains a decimal point
"12a"NumberFormatExceptionContains an invalid decimal digit
" 123 "NumberFormatExceptionContains surrounding spaces
""NumberFormatExceptionEmpty String
nullNumberFormatExceptionNo String value to parse
"2147483648"NumberFormatExceptionGreater than Integer.MAX_VALUE

Java String-to-int conversion summary

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

Use Integer.parseInt() when you need a primitive int, and use Integer.valueOf() when you need an Integer object. Both require the input to represent an integer that fits within the int range and throw NumberFormatException when the String cannot be parsed. For input with surrounding whitespace, normalize it first when that is appropriate for your application. Avoid the deprecated Integer(String) constructor in new Java code.