How to Convert String to int in Java

In this article, we will show four ways to convert Java String to primitive int with code examples.

A common requirement while programming in Java is to convert a String to an int. UI inputs in Web-based HTML, JSP, or Thymeleaf templates may transfer to backend Java applications as strings. It is the application developer’s responsibility to perform any String to int conversions to fulfill business logic, such as calculating discounts, storing age, and so on.

This is a common scenario to convert String to a wrapper Integer class or primitive int in Java programming language.

In this post, we will demonstrate converting String to a wrapper Integer class or primitive int in 4 ways.
  1. Convert using Integer.parseInt()
  2. Convert using Integer.valueOf()
  3. Convert using DecimalFormat
Among the above 3 ways, the Integer.parseInt() and Integer.valueOf() methods are frequently used to convert String to int in Java programs.

1. Convert using Integer.parseInt()

The Integer.parseInt() method is a widely used and straightforward way to convert a string to an int. It parses the string argument as a signed decimal integer and returns the corresponding primitive int value.

Here's an example:
public class StringToIntegerExample {
    public static void main(String[] args) {
        String str = "42";
        
        // Using Integer.parseInt()
        int result = Integer.parseInt(str);
        System.out.println("Parsed int value: " + result); // Output: Parsed int value: 42
    }
}

Output:

Parsed int value: 42

2. Convert using Integer.valueOf(): 

The Integer.valueOf() method is another option to convert a string to an Integer object. It takes a string as an argument and returns an Integer object representing the int value. 

Here's an example:
public class StringToIntegerExample {
    public static void main(String[] args) {
        String str = "77";
        
        // Using Integer.valueOf()
        Integer intValue = Integer.valueOf(str);
        System.out.println("Converted Integer value: " + intValue); // Output: Converted Integer value: 77
    }
}

Output:

Converted Integer value: 77

3. Convert using DecimalFormat: 

Another approach to convert a string to an int involves using the DecimalFormat class, although it's not a common method for this purpose. 

Here's an example:
import java.text.DecimalFormat;
import java.text.ParseException;

public class StringToIntegerExample {
    public static void main(String[] args) {
        String str = "123";
        
        // Using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#");
        try {
            Number number = decimalFormat.parse(str);
            int result = number.intValue();
            System.out.println("Converted int value: " + result); // Output: Converted int value: 123
        } catch (ParseException e) {
            System.out.println("Invalid input: " + str);
        }
    }
}

Output:

Converted int value: 123

Conclusion

Converting a string to an integer is a common and essential operation in Java. In this blog post, we explored different ways to achieve this conversion. The recommended methods are Integer.parseInt() and Integer.valueOf(), as they provide straightforward and efficient solutions for this task. Always ensure proper error handling when converting strings to integers to handle cases where the string is not a valid representation of an integer.

Comments