Converting String to Int in Java

To convert a string to an int in Java, use the Integer.parseInt() method. It reads the string as a signed decimal number and returns a primitive int. Integer.valueOf() parses the string the same way and returns an Integer object. If the string is not a valid integer, both methods throw a NumberFormatException. In this Java String to Int Example, we convert a string to an int with Integer.parseInt() and catch the NumberFormatException. Click Execute to run the Java String to Int Example online and see the result.
Converting String to Int in Java Execute
public class StringToIntDemo {
    public static void main(String[] args) {
        String numberString = "12345";

        try {
            int number = Integer.parseInt(numberString);
            System.out.println("Converted integer value: " + number);
        } catch (NumberFormatException e) {
            System.out.println("Error: The given string '" + numberString + "' is not a valid integer representation.");
        }
    }
}

Java Integer.parseInt() Syntax

parseInt() is a static method of the Integer class. The second form takes a radix: the base of the number written in the string.

Java Integer.parseInt() Syntax
public static int parseInt(String s)
public static int parseInt(String s, int radix)

What is the difference between parseInt() and valueOf()?

Integer.parseInt() returns a primitive int. Integer.valueOf() returns an Integer object with the same value. Both methods accept the same strings and throw the same NumberFormatException.

Java String to Int Examples

The following are examples of converting a string to int in Java. Each example runs in the online Java code runner.

Convert a string to int with Integer.parseInt()

parseInt() reads the string as a signed decimal number. A leading minus or plus sign is allowed.

Java parseInt() Example
public class ParseIntExample {
    public static void main(String[] args) {
        int a = Integer.parseInt("123");
        int b = Integer.parseInt("-45");
        System.out.println(a + b);
    }
}

// output: 78

Convert a string to Integer with Integer.valueOf()

valueOf() returns an Integer object instead of a primitive int.

Java valueOf() Example
public class ValueOfExample {
    public static void main(String[] args) {
        Integer number = Integer.valueOf("2026");
        System.out.println(number + 1);
    }
}

// output: 2027

Handle a NumberFormatException

parseInt() throws a NumberFormatException if the string contains anything other than digits and one leading sign. Spaces, decimal points, an empty string and numbers outside the int range all fail. Wrap the call in a try-catch block when the string comes from user input or a file.

Java NumberFormatException Example
public class NumberFormatExample {
    public static void main(String[] args) {
        String[] inputs = {"42", " 42", "4.2", "2147483648"};
        for (String input : inputs) {
            try {
                System.out.println(Integer.parseInt(input));
            } catch (NumberFormatException e) {
                System.out.println("Not an int: \"" + input + "\"");
            }
        }
    }
}

// output:
// 42
// Not an int: " 42"
// Not an int: "4.2"
// Not an int: "2147483648"

Convert a binary or hexadecimal string to int

Pass the radix as the second argument: 2 for binary, 8 for octal, 16 for hexadecimal.

Java parseInt() with Radix Example
public class RadixExample {
    public static void main(String[] args) {
        System.out.println(Integer.parseInt("1100110", 2));
        System.out.println(Integer.parseInt("-FF", 16));
    }
}

// output:
// 102
// -255
Updated: