Java Character GetType Codepoint Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Java getType(int codePoint) method of the Character class to get the general category of a Unicode codepoint character value. You will also learn how to write and execute Java code in the terminal of Ubuntu system.


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/break_continue("`Break/Continue`") 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/switch("`Switch`") 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-117499{{"`Java Character GetType Codepoint Method`"}} java/classes_objects -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/class_methods -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/exceptions -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/modifiers -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/oop -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/packages_api -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/user_input -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/wrapper_classes -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/identifier -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/arrays -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/break_continue -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/data_types -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/operators -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/output -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/strings -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/switch -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/variables -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/string_methods -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} java/system_methods -.-> lab-117499{{"`Java Character GetType Codepoint Method`"}} end

Set up the file structure

In the terminal, create a directory called project by running the following command:

mkdir project

Then navigate to the project directory:

cd project

Create a new file called GetTypeLab.java:

touch GetTypeLab.java

Open the file with your text editor:

touch GetTypeLab.java

Write the Java code

In the GetTypeLab.java file, write the following Java code:

import java.util.Scanner;

public class GetTypeLab {
    public static void main(String[] args) {
        try {
            System.out.println("Enter the Unicode codepoint character:");
            Scanner sc = new Scanner(System.in);
            int cp = sc.nextInt();
            int type = Character.getType(cp);
            switch (type) {
                case Character.COMBINING_SPACING_MARK:
                    System.out.println("Combining Spacing Mark");
                    break;
                case Character.CONNECTOR_PUNCTUATION:
                    System.out.println("Connector Punctuation");
                    break;
                case Character.CONTROL:
                    System.out.println("Control");
                    break;
                case Character.CURRENCY_SYMBOL:
                    System.out.println("Currency Symbol");
                    break;
                case Character.DASH_PUNCTUATION:
                    System.out.println("Dash Punctuation");
                    break;
                case Character.DECIMAL_DIGIT_NUMBER:
                    System.out.println("Decimal Digit Number");
                    break;
                case Character.ENCLOSING_MARK:
                    System.out.println("Enclosing Mark");
                    break;
                case Character.END_PUNCTUATION:
                    System.out.println("End Punctuation");
                    break;
                case Character.FINAL_QUOTE_PUNCTUATION:
                    System.out.println("Final Quote Punctuation");
                    break;
                case Character.FORMAT:
                    System.out.println("Format");
                    break;
                case Character.INITIAL_QUOTE_PUNCTUATION:
                    System.out.println("Initial Quote Punctuation");
                    break;
                case Character.LETTER_NUMBER:
                    System.out.println("Letter Number");
                    break;
                case Character.LINE_SEPARATOR:
                    System.out.println("Line Separator");
                    break;
                case Character.LOWERCASE_LETTER:
                    System.out.println("Lowercase Letter");
                    break;
                case Character.MATH_SYMBOL:
                    System.out.println("Math Symbol");
                    break;
                case Character.MODIFIER_LETTER:
                    System.out.println("Modifier Letter");
                    break;
                case Character.MODIFIER_SYMBOL:
                    System.out.println("Modifier Symbol");
                    break;
                case Character.NON_SPACING_MARK:
                    System.out.println("Non-Spacing Mark");
                    break;
                case Character.OTHER_LETTER:
                    System.out.println("Other Letter");
                    break;
                case Character.OTHER_NUMBER:
                    System.out.println("Other Number");
                    break;
                case Character.OTHER_PUNCTUATION:
                    System.out.println("Other Punctuation");
                    break;
                case Character.OTHER_SYMBOL:
                    System.out.println("Other Symbol");
                    break;
                case Character.PARAGRAPH_SEPARATOR:
                    System.out.println("Paragraph Separator");
                    break;
                case Character.PRIVATE_USE:
                    System.out.println("Private Use");
                    break;
                case Character.SPACE_SEPARATOR:
                    System.out.println("Space Separator");
                    break;
                case Character.START_PUNCTUATION:
                    System.out.println("Start Punctuation");
                    break;
                case Character.SURROGATE:
                    System.out.println("Surrogate");
                    break;
                case Character.TITLECASE_LETTER:
                    System.out.println("Titlecase Letter");
                    break;
                case Character.UNASSIGNED:
                    System.out.println("Unassigned");
                    break;
                case Character.UPPERCASE_LETTER:
                    System.out.println("Uppercase Letter");
                    break;
            }
        } catch (Exception e) {
            System.out.println("Invalid input.");
        }
    }
}

The Java code prompts the user to enter a Unicode codepoint character, uses the getType() method to get the general category of the character, and prints out the corresponding category.

Compile and run the code

Compile the GetTypeLab.java file by running the following command:

javac GetTypeLab.java

To run the code, enter the following command:

java GetTypeLab

When prompted, enter a Unicode codepoint character, such as "A" (without quotes), and press Enter.

Modify the code

Modify the code to print out the name of the general category instead of its ordinal value. You can achieve this by replacing the switch statement with the following code:

String typeName = Character.getName(type);
System.out.println("The character's general category is: " + typeName);

Compile and run the modified code. Observe how the output changes.

Summary

In this lab, you have learned how to use the Java getType(int codePoint) method of the Character class to get the general category of a Unicode codepoint character value. You have written Java code in the terminal of Ubuntu system and used the Scanner class to get input from the user. You have also seen how to modify the code to print out the name of the general category instead of its ordinal value.

Other Java Tutorials you may like