×

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
  1. public static int parseUnsignedInt (String s) throws NumberFormatException
  2. public static int parseUnsignedInt (String s, int radix) throws NumberFormatException
  3. 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
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
123
Example 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
1
Example 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)

Related Topics

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.