String to int in Java: parseInt, valueOf, and Surviving NumberFormatException

February 11, 2026
int num = Integer.parseInt("123");
num: 123

Converting String to int in Java: The Ultimate Guide

Want to turn a string into an integer in Java? Well, you're in luck! Converting a string to an int in Java is a pretty common task, and there are a few ways to do it. We'll cover the most popular methods in this post, including the infamous Integer.parseInt() method. So, if you're struggling with "string to int Java" conversions, keep reading!

Integer.parseInt(): The Most Popular Method for String to int Java Conversion

Integer.parseInt() is the most widely used method for converting a string to an int in Java. It's a static method that takes a string as input and returns the corresponding integer value. Here's an example:

public class Main {
    public static void main(String[] args) {
        String str = "123";
        int num = Integer.parseInt(str);
        System.out.println(num); // prints 123
    }
}
num: 123

NumberFormatException pain

Integer.valueOf(): Another Way to Convert String to int in Java

Integer.valueOf() is another method that can be used to convert a string to an int in Java. It's similar to Integer.parseInt(), but it returns an Integer object instead of a primitive int value. Here's an example:

public class Main {
    public static void main(String[] args) {
        String str = "123";
        Integer num = Integer.valueOf(str);
        System.out.println(num); // prints 123
    }
}
num: 123

Handling NumberFormatException: The Pain of String to int Java Conversion

What happens if you try to convert a string that's not a valid integer? Well, you'll get a NumberFormatException, that's what! This exception is thrown when the input string cannot be parsed into an integer. Here's an example:

public class Main {
    public static void main(String[] args) {
        String str = "abc";
        try {
            int num = Integer.parseInt(str);
            System.out.println(num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input");
        }
    }
}
Invalid input

parseInt works celebration

Edge Cases: What to Watch Out for When Converting String to int in Java

There are a few edge cases to watch out for when converting a string to an int in Java. For example:

  • Leading zeros: If the input string has leading zeros, they will be ignored. For example, "0123" will be parsed as 123.
  • Negative numbers: If the input string starts with a minus sign, it will be parsed as a negative number. For example, "-123" will be parsed as -123.
  • Overflow: If the input string represents a number that's too large to fit into an int, you'll get an exception.

Radix Parsing: Converting String to int with a Specific Radix

Integer.parseInt() also allows you to specify a radix, which is the base of the number system. For example, you can parse a hexadecimal string by specifying a radix of 16:

public class Main {
    public static void main(String[] args) {
        String str = "A";
        int num = Integer.parseInt(str, 16);
        System.out.println(num); // prints 10
    }
}
num: 10

Best Practices for String to int Java Conversion

Here are a few best practices to keep in mind when converting a string to an int in Java:

  • Always handle NumberFormatException exceptions.
  • Use Integer.parseInt() instead of Integer.valueOf() unless you need an Integer object.
  • Specify a radix if you're working with non-decimal numbers.

And there you have it! Converting a string to an int in Java is a breeze once you know the right methods to use. So, go forth and parse those strings like a pro!

Want to convert more than just strings to integers? Check out CodeConverter, the ultimate code conversion tool!

Related Articles