Convert a String to an int in Java

How to convert a String to an int in java?

The Java Integer wrapper class provides a method parseInt to convert a String to an int.  Here is the method signature:

  public static int parseInt(String s) throws NumberFormatException

One thing that you will note in the declaration is that the method also throws NumberFormatException.  Just note it for now, we will see below why this exception is thrown and how to handle it. Let us start with a very simple example of converting a String to int.

Simple Example

Let us forget about the exception for now and write a program that can do a conversion for us.

package com.programtalk.beginner.tutorial;
public class ConvertString2Integer {
  public static void main(String[] args) {
   String str = "1234";
   int intVal = Integer.parseInt(str);
   System.out.println(intVal); // prints 1234

If you have already executed the above program, try changing the str value to "test" and see what happens.  You would see that the program no longer prints anything but throws an exception

Exception in thread "main" java.lang.NumberFormatException: For input string: "test"
 at java.lang.NumberFormatException.forInputString(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at com.programtalk.beginner.tutorial.ConvertString2Integer.main(

The stacktrace shows that the method has thrown NumberFormatException as we passed in a String "test" that can’t be converted to an int data type.

String to int example with exception handling

Now we will create a java class that will handle NumberFormatException in case it occurs. Additional steps added in this example

  1. Handle the null String. If null is passed to parseInt, it will throw  NullPointerException
  2. NumberFormatException is caught and a message is printed. If a string that is not a number like “test” is passed to  parseInt then NumberFormatException will be thrown
  3.  trim() is used to handle a space at the end or before the String like " 23 " will be trimmed to "23" and converted to int 23. If we don’t trim the String ” 23 “, parseInt will throw NumberFormatException


package com.programtalk.beginner.tutorial;

public class ConvertString2Integer {
	public static void main(String[] args) {
		// passing a null will show the message "Cannot convert null to int"

		// this would convert 23 to int 

		// In this case the trim() will make sure that the spaces are removed
		convertStringToInt(" 23 ");
		// this would throw exception

	private static void convertStringToInt(String str) {
		// handle the null value
		if(str == null){
			System.out.println("Cannot convert null to int");
		try {
			// convert to int and also trim the string
			int intVal = Integer.parseInt(str.trim());
			System.out.println(intVal); // prints 20
		} catch (NumberFormatException ex) {
			System.out.println("NumberFormatException while conversion: " + ex.getMessage());


Cannot convert null to int
NumberFormatException while conversion: For input string: "test"

Additional points

Convert String to Integer Wrapper class

If you want to convert s String to the Integer wrapper class that can be done by using valueOf() method of Integer class.

Integer intVal = Integer.valueOf("23");



So in this tutorial we saw how to convert String to int. I hope that has helped you. Just drop a note in the below comment section in case of questions or comments.

Like this post? Don’t forget to share it!