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.

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