Java Integer parseUnsignedInt Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the parseUnsignedInt method present in the Integer class of the java.lang package. This method helps to parse the character sequence into an unsigned integer value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/classes_objects -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/class_methods -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/exceptions -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/modifiers -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/oop -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/packages_api -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/user_input -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/wrapper_classes -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/identifier -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/arrays -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/data_types -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/operators -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/output -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/strings -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/variables -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/string_methods -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} java/system_methods -.-> lab-117732{{"`Java Integer parseUnsignedInt Method`"}} end

Create a Java file

Create a new Java file with the name ParseUnsignedIntLab.java in the ~/project directory using the following command:

touch ~/project/ParseUnsignedIntLab.java

Import package and Define Class

Import the required java.util and java.lang packages and define the ParseUnsignedIntLab class.

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

public class ParseUnsignedIntLab {
    public static void main(String[] args) {

    }
}

Read input from user

Use Scanner class to read input from the user and store it into a String named text. Then, read the radix and start (beginIndex) and end (endIndex) indices of the substring from the user.

Scanner sc = new Scanner(System.in);

System.out.print("Enter a character sequence: ");
String text = sc.nextLine();

System.out.print("Enter the integer radix: ");
int radix = sc.nextInt();

System.out.print("Enter the start index: ");
int beginIndex = sc.nextInt();

System.out.print("Enter the end index: ");
int endIndex = sc.nextInt();

Parse the character sequence

Use the parseUnsignedInt method to parse the substring of the given character sequence with the given radix.

int result = Integer.parseUnsignedInt(text, beginIndex, endIndex, radix);
System.out.println("Unsigned integer value: " + result);

Handle exceptions

Handle the exceptions with try-catch blocks.

try {
    int result = Integer.parseUnsignedInt(text, beginIndex, endIndex, radix);
    System.out.println("Unsigned integer value: " + result);
} catch (NumberFormatException e) {
    System.out.println("Cannot parse the input string");
} catch (IndexOutOfBoundsException e) {
    System.out.println("The start or end index is invalid");
}

Compile and run the program

Compile the program using the following command:

javac ParseUnsignedIntLab.java

Run the program using the following command:

java ParseUnsignedIntLab

Summary

In this lab, you have learned about the parseUnsignedInt method present in the Integer class of the java.lang package. You have learned to use this method to parse a character sequence into an unsigned integer value. Additionally, you have learned to handle exceptions in case of incorrect input or invalid indices values were provided.