Identifying Unicode Identifier Start Characters

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the isUnicodeIdentifierStart(char ch) method of the Character class. This method checks whether a given character is allowed as the first character in a Unicode identifier or not. You will also learn how to write a Java program to test the isUnicodeIdentifierStart(char ch) method.

Create Java file

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

touch ~/project/UnicodeIdentifierStart.java

This command opens the Nano text editor to create a new Java file named UnicodeIdentifierStart.java.

Write Java code

In this step, you will write Java code to test the isUnicodeIdentifierStart(char ch) method.

Add the following code to the UnicodeIdentifierStart.java file:

import java.util.Scanner;

public class UnicodeIdentifierStart {
    public static void main(String[] args) {

        // Initialize Scanner
        Scanner scanner = new Scanner(System.in);

        // Ask user for input
        System.out.print("Enter a character: ");

        // Read character from user
        char ch = scanner.next().charAt(0);

        // Check if char is allowed as first character of a Unicode identifier
        boolean isUnicodeIdentifierStart = Character.isUnicodeIdentifierStart(ch);

        // Print result
        System.out.println("'" + ch + "'" + " is a start Unicode identifier? " + isUnicodeIdentifierStart);

        // Close scanner
        scanner.close();
    }
}

The above code will ask the user to enter a character, read the character entered by the user, check whether it is allowed as the first character of a Unicode identifier, and print the result accordingly.

Compile Java code

In this step, you will compile the Java code using the following command:

javac ~/project/UnicodeIdentifierStart.java

This command will compile the UnicodeIdentifierStart.java file and generate a bytecode file named UnicodeIdentifierStart.class in the same directory.

Run Java program

In this step, you will run the Java program using the following command:

java UnicodeIdentifierStart

This command will run the compiled UnicodeIdentifierStart.class file and display the following message:

Enter a character:

Once you enter a character, the program checks whether it is allowed as the first character of a Unicode identifier or not, and displays the result accordingly.

Modify Java code

You can modify the Java code to test the isUnicodeIdentifierStart(char ch) method for different characters.

For example, you can modify the following line of code:

char ch = scanner.next().charAt(0);

to:

char ch = 'Ãą';

This will test whether the character 'Ãą' is allowed as the first character of a Unicode identifier or not.

Recompile Java code

After modifying the Java code, you need to recompile it using the following command:

javac ~/project/UnicodeIdentifierStart.java

This command will recompile the modified UnicodeIdentifierStart.java file and generate a new bytecode file named UnicodeIdentifierStart.class in the same directory.

Rerun Java program

After recompiling the Java code, you need to rerun the program using the following command:

java UnicodeIdentifierStart

This command will run the newly compiled UnicodeIdentifierStart.class file and display the following message:

Enter a character:

Once you enter the character, the program checks whether it is allowed as the first character of a Unicode identifier or not, and displays the result accordingly.

Modify Java code to check multiple characters

You can modify the Java code to check multiple characters by using a loop. For example, you can modify the following code:

// Ask user for input
System.out.print("Enter a character: ");

// Read character from user
char ch = scanner.next().charAt(0);

// Check if char is allowed as first character of a Unicode identifier
boolean isUnicodeIdentifierStart = Character.isUnicodeIdentifierStart(ch);

// Print result
System.out.println("'" + ch + "'" + " is a start Unicode identifier? " + isUnicodeIdentifierStart);

to:

// Ask user for input
System.out.print("Enter characters: ");

// Read input from user
String input = scanner.nextLine();

// Loop through each character in input
for (char ch : input.toCharArray()) {

    // Check if char is allowed as first character of a Unicode identifier
    boolean isUnicodeIdentifierStart = Character.isUnicodeIdentifierStart(ch);

    // Print result
    System.out.println("'" + ch + "'" + " is a start Unicode identifier? " + isUnicodeIdentifierStart);
}

This will ask the user to enter multiple characters, loop through each character and check whether it is allowed as the first character of a Unicode identifier or not, and print the result accordingly.

Recompile and rerun Java program

After modifying the Java code to check multiple characters, you need to recompile it using the following command:

javac ~/project/UnicodeIdentifierStart.java

This command will recompile the modified UnicodeIdentifierStart.java file and generate a new bytecode file named UnicodeIdentifierStart.class in the same directory.

After recompiling the Java code, you need to rerun the program using the following command:

java UnicodeIdentifierStart

This command will run the newly compiled UnicodeIdentifierStart.class file and display the following message:

Enter characters:

Once you enter the characters, the program checks whether each character is allowed as the first character of a Unicode identifier or not, and displays the result accordingly.

Summary

Congratulation! You have successfully learned how to use the isUnicodeIdentifierStart(char ch) method of the Character class to check whether a given character is allowed as the first character in a Unicode identifier or not. You have also learned how to write a Java program to test this method.

Other Java Tutorials you may like