Identify Unicode Identifier Parts in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the isUnicodeIdentifierPart(int codePoint) method of the Character class in Java. This method is used to check whether a specified Unicode character is a part of a Unicode identifier or not. A Unicode identifier is a sequence of characters that is used to uniquely identify an entity in a program.

Create a new Java file

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

touch ~/project/UnicodeIdentifier.java

Define the main method

In this step, define the main method and import the java.util.Scanner class as shown in the code block below.

import java.util.Scanner;

public class UnicodeIdentifier {

    public static void main(String[] args) {

    }
}

Get the Unicode character from user input

In this step, get the value of a Unicode character from the user input using a Scanner class. Add the below code inside the main method.

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a Unicode character: ");
    int unicodeValue = scanner.next().codePointAt(0);

Check if the Unicode character is a part of a Unicode identifier

Use the isUnicodeIdentifierPart(int codePoint) method of the Character class to check if the specified Unicode character is a part of a Unicode identifier or not. Add the below code inside the main method.

    boolean isUnicodeIdentifier = Character.isUnicodeIdentifierPart(unicodeValue);

Print the output

Use the System.out.println() method to print the result to the console. Add the following code inside the main method.

    System.out.println((char) unicodeValue + " is a part of a Unicode identifier: " + isUnicodeIdentifier);

Compile and run the program

Compile the program using the following command:

javac ~/project/UnicodeIdentifier.java

Run the program using the following command:

java UnicodeIdentifier

Summary

In this lab, you have learned how to check if a character is a part of a Unicode identifier using the isUnicodeIdentifierPart(int codePoint) method of the Character class in Java. You have also learned how to get user input and print the output to the console using the Scanner class and System class respectively.

Other Java Tutorials you may like