Java Character forDigit Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the Java forDigit() method which is a part of the Character class. This method returns the specified character representation for the specified digit in accordance with the specified 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/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") 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/if_else("`If...Else`") 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-117491{{"`Java Character forDigit Method`"}} java/classes_objects -.-> lab-117491{{"`Java Character forDigit Method`"}} java/class_methods -.-> lab-117491{{"`Java Character forDigit Method`"}} java/modifiers -.-> lab-117491{{"`Java Character forDigit Method`"}} java/oop -.-> lab-117491{{"`Java Character forDigit Method`"}} java/user_input -.-> lab-117491{{"`Java Character forDigit Method`"}} java/wrapper_classes -.-> lab-117491{{"`Java Character forDigit Method`"}} java/identifier -.-> lab-117491{{"`Java Character forDigit Method`"}} java/arrays -.-> lab-117491{{"`Java Character forDigit Method`"}} java/data_types -.-> lab-117491{{"`Java Character forDigit Method`"}} java/if_else -.-> lab-117491{{"`Java Character forDigit Method`"}} java/operators -.-> lab-117491{{"`Java Character forDigit Method`"}} java/output -.-> lab-117491{{"`Java Character forDigit Method`"}} java/strings -.-> lab-117491{{"`Java Character forDigit Method`"}} java/variables -.-> lab-117491{{"`Java Character forDigit Method`"}} java/system_methods -.-> lab-117491{{"`Java Character forDigit Method`"}} end

Create a Java File

Create a Java file named CharForDigit.java in the ~/project directory using the following command:

touch ~/project/CharForDigit.java

Add the Initial Code

Add the following code to the CharForDigit.java file:

public class CharForDigit {
    public static void main(String[] args) {
        int n1 = 8;
        int n2 = 23;
        int n3 = 30;
        char ch1 = Character.forDigit(n1, 9);
        char ch2 = Character.forDigit(n2, 25);
        char ch3 = Character.forDigit(n3, 36);
        System.out.println("The char representation of " + n1 + " in radix 9 is " + ch1);
        System.out.println("The char representation of " + n2 + " in radix 25 is " + ch2);
        System.out.println("The char representation of " + n3 + " in radix 36 is " + ch3);
    }
}

This code creates a CharForDigit class with a main method. The main method demonstrates how to use the forDigit() method to get the character representation of a digit in different radix.

Compile and Run the Code

Compile the CharForDigit.java file using the following command:

javac CharForDigit.java

Then, run the code using the following command:

java CharForDigit

You should see the following output:

The char representation of 8 in radix 9 is 8
The char representation of 23 in radix 25 is n
The char representation of 30 in radix 36 is u

This output confirms that the forDigit() method works as expected.

Add User Input

Now, let's modify the code to accept user input for the digit and radix. Add the following code after the initial code:

Scanner sc = new Scanner(System.in);
System.out.print("Enter digit: ");
int d = sc.nextInt();
System.out.print("Enter radix: ");
int r = sc.nextInt();
char result = Character.forDigit(d, r);
System.out.println("The character at radix \'" + r + "\' for the digit " + d + " is: " + result);

This code prompts the user for the digit and radix, reads the input from the console, and uses the forDigit() method to get the character representation of the digit.

Compile and Run the Code with User Input

Compile the modified CharForDigit.java file using the following command:

javac CharForDigit.java

Then, run the code using the following command:

java CharForDigit

You should see the following prompt in the console:

Enter digit:

Enter a digit (e.g. 12) and press Enter. You should see the following prompt:

Enter radix:

Enter a radix (e.g. 16) and press Enter. You should then see the following output:

The character at radix '16' for the digit 12 is: c

This output confirms that the modified code works as expected.

Test for Invalid Input

Modify the code to handle invalid user input. Add the following code after the user input code:

} catch (InputMismatchException e) {
    System.out.println("Invalid input!");
}

This code catches any InputMismatchException that is thrown by the Scanner class when the user enters input that cannot be parsed as an integer.

Compile and Run the Modified Code

Compile the modified CharForDigit.java file using the following command:

javac CharForDigit.java

Then, run the code using the following command:

java CharForDigit

You should see the following prompt in the console:

Enter digit:

Enter a non-numeric input (e.g. "abc") and press Enter. You should see the following output:

Invalid input!

This output confirms that the code handles invalid input as expected.

Test for Invalid Radix

Modify the code to check if the entered radix is valid. Add the following code after the user input code:

if (r < Character.MIN_RADIX || r > Character.MAX_RADIX) {
    System.out.println("Invalid radix!");
    return;
}

This code checks if the entered radix is less than the minimum radix or greater than the maximum radix. If so, it prints a message and terminates the program.

Compile and Run the Modified Code

Compile the modified CharForDigit.java file using the following command:

javac CharForDigit.java

Then, run the code using the following command:

java CharForDigit

You should see the following prompt in the console:

Enter digit:

Enter a digit (e.g. 6) and press Enter. You should see the following prompt:

Enter radix:

Enter a radix less than 2 or greater than 36 (e.g. 1 or 100) and press Enter. You should see the following output:

Invalid radix!

This output confirms that the code handles invalid radix as expected.

Summary

In this lab, you learned how to use the forDigit() method of the Character class to get the character representation of a digit in a specified radix. You also learned how to handle user input and check for valid input.

Other Java Tutorials you may like