The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer.
The second parameter parses the string argument as an unsigned integer in the radix specified by the second argument.
The third parameter parses the CharSequence argument as an unsigned as a signed int in the specified radix, beginning at the specified beginIndex and extending to endIndex -1.
Syntax
- public static int parseUnsignedInt (String s) throws NumberFormatException
- public static int parseUnsignedInt (String s, int radix) throws NumberFormatException
- public static int parseUnsignedInt (CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException
Parameters
s- a String containing the unsigned 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 unsigned integer value represented by the argument in decimal or in the specified radix.
Throws
This method throws:
NumberFormatException- if the String 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 unsigned int in the specified radix, or if radix is either smaller than Character.MAX_RADIX or larger than Character.MAX_RADIX.
Example 1
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerparseUnsignedIntExample1 { public static void main(String[] args) { String s="123"; //It parses the string argument as an unsigned decimal integer. int val=Integer.parseUnsignedInt(s); System.out.println(val); } } |
Output
1 2 3 |
123 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 |
public class JavaIntegerparseUnsignedIntExample2 { public static void main(String[] args) { String s="123"; //radix should be smaller than Character.MAX_RADIX i.e. 36 else it will give you an error int radix=37; int val=Integer.parseUnsignedInt(s,radix); System.out.println(val); } } |
Output
1 2 3 4 5 6 |
Exception in thread "main" java.lang.NumberFormatException: radix 37 greater than Character.MAX_RADIX at java.lang.Integer.parseInt(Integer.java:551) at java.lang.Integer.parseUnsignedInt(Integer.java:677) at com.TutorialAndExample.JavaIntegerparseUnsignedIntExample2.main(JavaIntegerparseUnsignedIntExample2.java:8) |
Example 3
1 2 3 4 5 6 7 8 9 10 11 |
public class JavaIntegerparseUnsignedIntExample3 { public static void main(String[] args) { String s="1"; //radix should be smaller than Character.MAX_RADIX i.e. 36 else it will give you an error int radix=3; int val=Integer.parseUnsignedInt(s,radix); System.out.println(val); } } |
Output
1 2 3 |
1 |
Example 4
1 2 3 4 5 6 7 8 9 10 11 |
public class JavaIntegerparseUnsignedIntExample4 { public static void main(String[] args) { //give NumberFormatException if the String does not contain a parsable int String s=null; int radix=37; int val=Integer.parseUnsignedInt(s,radix); System.out.println(val); } } |
Output
1 2 3 4 5 |
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseUnsignedInt(Integer.java:664) at com.TutorialAndExample.JavaIntegerparseUnsignedIntExample4.main(JavaIntegerparseUnsignedIntExample4.java:11) |