Java Character Digit Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the digit() method provided by the Character class in Java. This method of the Character class is used to convert a character to its numeric value based on the provided radix.


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/comments("`Comments`") 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/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117487{{"`Java Character Digit Method`"}} java/classes_objects -.-> lab-117487{{"`Java Character Digit Method`"}} java/class_methods -.-> lab-117487{{"`Java Character Digit Method`"}} java/exceptions -.-> lab-117487{{"`Java Character Digit Method`"}} java/modifiers -.-> lab-117487{{"`Java Character Digit Method`"}} java/oop -.-> lab-117487{{"`Java Character Digit Method`"}} java/packages_api -.-> lab-117487{{"`Java Character Digit Method`"}} java/user_input -.-> lab-117487{{"`Java Character Digit Method`"}} java/wrapper_classes -.-> lab-117487{{"`Java Character Digit Method`"}} java/identifier -.-> lab-117487{{"`Java Character Digit Method`"}} java/arrays -.-> lab-117487{{"`Java Character Digit Method`"}} java/comments -.-> lab-117487{{"`Java Character Digit Method`"}} java/data_types -.-> lab-117487{{"`Java Character Digit Method`"}} java/operators -.-> lab-117487{{"`Java Character Digit Method`"}} java/output -.-> lab-117487{{"`Java Character Digit Method`"}} java/strings -.-> lab-117487{{"`Java Character Digit Method`"}} java/variables -.-> lab-117487{{"`Java Character Digit Method`"}} java/system_methods -.-> lab-117487{{"`Java Character Digit Method`"}} end

Creating the Java file

Create a new Java file named CharacterDigit.java in the ~/project directory by executing the following command in the terminal.

touch ~/project/CharacterDigit.java

Writing the Java code

Add the following code to the CharacterDigit.java file to use the digit() method to convert a character to its numeric value.

import java.util.Scanner;
import java.lang.Character;

public class CharacterDigit {

    public static void main(String[] args) {

        try {
            // Step 2.1: Getting character codepoint and radix from the user
            System.out.print("Enter character codepoint value:");
            Scanner sc = new Scanner(System.in);
            int codePoint = sc.nextInt();
            System.out.print("Enter radix:");
            int radix = sc.nextInt();

            // Step 2.2: Converting character to its numeric value based on radix
            int digit = Character.digit(codePoint, radix);

            // Step 2.3: Displaying the result to the user
            System.out.println("The numeric value is: " + digit);
        } catch (Exception e) {
            System.out.println("Invalid input");
        }
    }
}

Compiling and Running the Java code

Compile the CharacterDigit.java file by executing the following command in the terminal.

javac ~/project/CharacterDigit.java

After a successful compilation, execute the following command to run the Java program:

java -cp ~/project CharacterDigit

You will see a prompt asking you to enter the character codepoint value and radix. Enter the desired values and press enter. The program will convert the entered character to its numeric value based on the radix provided.

Summary

In this lab, you learned how to use the digit() method provided by the Character class in Java. This method is used to convert a character to its numeric value based on the provided radix. You also learned the steps required to create a Java program to use this method and how to run and test it in the terminal.

Other Java Tutorials you may like