Java Integer parseInt() MethodThe 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:
1. Java Integer parseInt (String s) MethodThis 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) MethodThis 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: Parameter:
Returns:
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 9:
Example 1Test it NowOutput: Value = 20 Value = 20 Value = -20 Example 2Test it NowOutput: Value = 104 Value = 512 Value = -484 Example 3Test it NowOutput: 200100 300 Example 4Test it NowOutput: int i = 100 Example 5Test it NowOutput: 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 TopicparseUnsignedInt() Method
|