Java Integer parseInt() Method

The parseInt() method is a method of Integer class under java.lang package. There are three different types of Java Integer parseInt () methods which can be differentiated depending on its parameter.

These are:

  • Java Integer parseInt (String s) Method
  • Java Integer parseInt (String s, int radix) Method
  • a Integer parseInt(CharSequence s, int beginText, int endText, int radix)
  • 1. Java Integer parseInt (String s) Method

    This method parses the String argument as a signed decimal integer object. The characters in the string must be decimal digits, except that the first character of the string may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus '+' sign to indicate a positive value. It returns the integer value which is represented by the argument in a decimal integer.

    2. Java Integer parseInt (String s, int radix) Method

    This method parses the String argument as a signed decimal integer object in the specified radix by the second argument. The characters in the string must be decimal digits of the specified argument except that the first character may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus sign '+' to indicate a positive value. The resulting integer value is to be returned.

    3. Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)

    This method parses the CharSequence argument as a signed integer in the specified radix argument, beginning at the specified beginIndex and extending to endIndex - 1 . This method does not take steps to guard against the CharSequence being mutated while parsing.

    Syntax:

    Following are the declarations of parseInt () method:

    public static int parseInt (String s) public static int parseInt (String s, int radix) public static int parseInt (CharSequence s, int beginIndex, int endIndex, int radix)

    Parameter:

    DataType Parameter Description Required/Optional String It is a String which needs to be converted into the Integer equivalent. Required radix The radix to be used while parsing the String Required beginIndex The beginning index, inclusive. Required endIndex The ending index, exclusive. Required CharSequence It is the CharSequence which needs to be converted into the Integer equivalent. Required

    Returns:

    Method Returns parseInt (String s) This method returns the integer value which is represented by the argument in decimal equivalent. parseInt (String s, int radix) This method returns the integer value which is represented by the string argument in the specified radix. parseInt (String s, int radix) This method returns the integer value which is represented by the string argument in the specified radix.

    Exceptions:

    NullPointerException: If s is null.

    IndexOutOfBoundsException: If beginIndex is negative, or if beginIndex is greater than endIndex or if endIndex is greater than s.length ().

    NumberFormatException: If the CharSequence does not contain a parsable int in the specified radix, or if radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.

    Compatibility Version:

    Java 1.2 and above:

  • Java Integer parseInt (String s)
  • Java Integer parseInt (String s, int radix)
  • Java 9:

  • Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)
  • Example 1

    public class IntegerParseIntExample1 { public static void main(String[] args) { int decimalExample = Integer.parseInt("20"); int signedPositiveExample = Integer.parseInt("+20"); int signedNegativeExample = Integer.parseInt("-20"); System.out.println("Value = "+decimalExample); System.out.println("Value = "+signedPositiveExample); System.out.println("Value = "+signedNegativeExample); Test it Now

    Output:

    Value = 20 Value = 20 Value = -20

    Example 2

    public class IntegerParseIntRadixExample2 { public static void main(String[] args) { int a = Integer.parseInt("150", 8); int b = Integer.parseInt("+200", 16); int c = Integer.parseInt("-344", 12); System.out.println("Value = "+a); System.out.println("Value = "+b); System.out.println("Value = "+c); Test it Now

    Output:

    Value = 104 Value = 512 Value = -484

    Example 3

    public class IntegerParseIntExample3 { public static void main(String[] args) { String s="200"; int i=Integer.parseInt(s); System.out.println(s+100);//200100 because + is string concatenation operator System.out.println(i+100);//300 because + is binary plus operator Test it Now

    Output:

    200100

    Example 4

    public class IntegerParseIntExample4 { public static void main(String[] args) { String s = "100"; // the String to int conversion happens here int i = Integer.parseInt(s.trim()); // print out the value after the conversion System.out.println("int i = " + i); catch (NumberFormatException nfe) System.out.println("NumberFormatException: " + nfe.getMessage()); Test it Now

    Output:

    int i = 100

    Example 5

    public class IntegerParseIntExample5 { public static void main(String[] args) { String number = "10A"; int result = Integer.parseInt(number); System.out.println(result); Test it Now

    Output:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "10A" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at myPackage.IntegerParseIntExample5.main(IntegerParseIntExample5.java:6) Next Topic parseUnsignedInt() Method