Java Integer parseInt() method

tutorial and example
tutorial and example

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer.

The second parameter parses the string argument as a signed integer in the radix specified by the second argument.

The third parameter parses the CharSequence argument as a signed int in the specified radix, beginning at the specified beginIndex and extending to endIndex -1.

Syntax

  1. public static int parseInt(String s) throws NumberFormatException
  2. public static int parseInt(String s, int radix) throws NumberFormatException
  3. public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException

Parameters

s- a String containing the int representation to be parsed.

radix- the radix to be used while parsing s.

beginIndex- the beginning index.

endIndex- the ending index.

 Return Value

This method returns an integer value represented by the argument in decimal or by the subsequence in the specified radix.

Throws

This method throws:

NumberFormatException– if the String does not contain a parsable int.

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.MAX_RADIX or larger than Character.MAX_RADIX.

 Example 1

Output

Example 2

Output

Example 3

Output

Example 4

Output