Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java parseUnsignedInt(String s) Method

Java parseUnsignedInt(String s) method is the part of the Integer class of the java.lang package. This method is used to parse the string value as an unsigned decimal Integer object.

It must be noted that the characters passed in the string must be all decimal except for the first character which is used to define the sign of the integer. The ASCII plus '+' sign is used to depict a positive value.

Syntax:

public static int parseUnsignedInt(String s) throws NumberFormatException

Parameters:

The parameter passed is the string value whose unsigned decimal Integer object is to be returned.

Returns:

Returns the unsigned decimal Integer object of the string value passed as a parameter.

Example 1:

Here, two strings are passed and are returned as their respective unsigned decimal Integer objects.

import java.lang.Integer;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
        String s1 = "23"; 
        String s2 = "132";
        int n1 = Integer.parseUnsignedInt(s1);  //converts the passed string as unsigned integer
        int n2 = Integer.parseUnsignedInt(s2);  //converts the passed string as unsigned integer
        
        System.out.println("Equivalent Integer object is : " + n1);
        System.out.println("Equivalent Integer object is : " + n2);    
    }  
}  


Equivalent Integer object is : 23
Equivalent Integer object is : 132

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Integer;
import java.util.Scanner;

public class StudyTonight {
  public static void main(String[] args) {
    try {
      System.out.println("Enter Value");
      Scanner sc = new Scanner(System. in );
      String s = sc.nextLine();
      int n = Integer.parseUnsignedInt(s); //converts the passed string as unsigned integer

      System.out.println("Equivalent Integer object is : " + n);
    }
    catch(Exception e) {
      System.out.println("Invalid Input!!");
    }
  }
}


Enter Value
45
Equivalent Integer object is : 45
*********************************
Enter Value
-34
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.