Java Integer parseUnsignedInt() method
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
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
123Example 2
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
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
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
1Example 4
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
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseUnsignedInt(Integer.java:664) at com.TutorialAndExample.JavaIntegerparseUnsignedIntExample4.main(JavaIntegerparseUnsignedIntExample4.java:11)