Java Integer parseInt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.parseInt(s) reads the text in s and returns the whole number it describes as a primitive int. It is the standard way to turn user input, file data, or a command-line argument into a number you can calculate with.

The text may start with - or + and must then contain only digits. A second argument chooses the base, so parseInt("ff", 16) reads hexadecimal. Any text that is not a valid int throws a NumberFormatException.

A static method of java.lang.Integer: call it on the class, as Integer.parseInt(...). Integer lives in java.lang, so no import is needed.

Syntax

Pass the text, and optionally the radix (the base of the number).

Java
int a = Integer.parseInt("42");        // 42
int b = Integer.parseInt("-17");       // -17
int c = Integer.parseInt("ff", 16);    // 255
CallWhat it does
Integer.parseInt(s)Reads s as a decimal int.
Integer.parseInt(s, radix)Reads s in the given base, from 2 to 36.
Integer.parseInt(s.trim())Removes surrounding spaces first, because spaces are not allowed.
Integer.valueOf(s)Same parsing, but returns an Integer object.

parseInt accepts no spaces, no decimal point, and no thousands separators. Java 9 adds a form that reads a slice of a CharSequence; the examples here stay on Java 8.

Library declarations
Java
// java.lang.Integer
public static int parseInt(String s) throws NumberFormatException
public static int parseInt(String s, int radix) throws NumberFormatException

// Java 9 and later
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)

Parameters

ParameterDescription
s (String)The text to read. It must not be null or empty.
radix (int)The base of the number, from Character.MIN_RADIX (2) to Character.MAX_RADIX (36). The one-argument form uses 10.

Return Value

Return ValueDescription
intThe number the text describes, between Integer.MIN_VALUE and Integer.MAX_VALUE.

Compare with Similar Methods

Other ways to turn text into a number:

MethodHow it differs
valueOf(s)Parses the same way but returns an Integer object instead of an int.
decode(s)Understands the 0x, #, and leading 0 prefixes.
parseUnsignedInt(s)Accepts values up to 4294967295 and wraps them into an int.
Double.parseDouble(s)Needed when the text has a decimal point.

Java Examples

Example 1 — Turn Text Quantities into a Total

Parse a few quantities written as text, including a sign, and add them up.

Java
public class IntegerParseIntDemo {
    public static void main(String[] args) {
        String[] quantities = {"3", "12", "-4", "+8"};
        int total = 0;

        for (String text : quantities) {
            int value = Integer.parseInt(text);
            System.out.println("\"" + text + "\" -> " + value);
            total += value;
        }
        System.out.println("total: " + total);
    }
}

How It Works

  1. One call per string. The loop hands each piece of text to parseInt, which returns a real int.
  2. Signs are fine. "-4" becomes -4, and "+8" becomes 8 because a leading plus is accepted.
  3. Now it is arithmetic. Because the values are int values, total += value adds them as numbers, not as joined text.
  4. The sum. 3 + 12 - 4 + 8 gives 19.

Example 2 — Read Binary, Octal, Hex, and Base 36 Text

Pass a radix as the second argument to read numbers that are not written in base 10.

Java
public class IntegerParseIntRadix {
    public static void main(String[] args) {
        System.out.println("1010 in base 2:  " + Integer.parseInt("1010", 2));
        System.out.println("777 in base 8:   " + Integer.parseInt("777", 8));
        System.out.println("ff in base 16:   " + Integer.parseInt("ff", 16));
        System.out.println("FF in base 16:   " + Integer.parseInt("FF", 16));
        System.out.println("zz in base 36:   " + Integer.parseInt("zz", 36));
        System.out.println("-101 in base 2:  " + Integer.parseInt("-101", 2));
    }
}

How It Works

  1. Binary. "1010" in base 2 is 8 + 2, which is 10.
  2. Octal. "777" in base 8 is 7 × 64 + 7 × 8 + 7, which is 511.
  3. Hex ignores case. "ff" and "FF" both read as 255, because letters a to f are accepted in either case.
  4. Base 36 uses every letter. "zz" is 35 × 36 + 35, which is 1295, and a leading minus still works.

Example 3 — Catch NumberFormatException for Bad Input

Try a mix of good and bad text, and print which ones parseInt rejects.

Java
public class IntegerParseIntInvalid {
    static String tryParse(String text) {
        try {
            return String.valueOf(Integer.parseInt(text));
        } catch (NumberFormatException e) {
            return "NumberFormatException";
        }
    }

    public static void main(String[] args) {
        String[] inputs = {"12", " 12", "12.5", "", "abc", "2147483647", "2147483648", "-2147483648", null};
        for (String text : inputs) {
            String label = text == null ? "null" : "\"" + text + "\"";
            System.out.println(label + " -> " + tryParse(text));
        }
    }
}

How It Works

  1. Wrap the call. tryParse returns the number as text when the parse works, and the exception name when it does not.
  2. Strict rules. A leading space, a decimal point, an empty string, letters, and null are all rejected.
  3. The int range. 2147483647 is Integer.MAX_VALUE and is accepted, but 2147483648 is one too large and fails.
  4. The negative end. -2147483648 is Integer.MIN_VALUE, which has no positive twin, yet parseInt reads it correctly.

Frequently Asked Questions

Call Integer.parseInt(text). It returns a primitive int, or throws NumberFormatException if the text is not a valid whole number.
parseInt does not ignore spaces. Call text.trim() first if the input may have leading or trailing whitespace.
parseInt returns a primitive int, while Integer.valueOf returns an Integer object. Both parse the text with the same rules.
No, "3.5" throws NumberFormatException. Use Double.parseDouble for decimals, or read the whole part yourself.
Pass the base as the second argument: Integer.parseInt("ff", 16) gives 255 and Integer.parseInt("1010", 2) gives 10. Do not include a 0x prefix; decode() handles prefixes.

Next: parseUnsignedInt()

Read text as an unsigned 32-bit number.

parseUnsignedInt() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful