×

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

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.