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.


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/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/comments("`Comments`") 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/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/classes_objects -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/class_methods -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/modifiers -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/oop -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/packages_api -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/user_input -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/wrapper_classes -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/identifier -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/arrays -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/comments -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/data_types -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/operators -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/output -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/strings -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/variables -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/string_methods -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} java/system_methods -.-> lab-117563{{"`Identifying Unicode Identifier Start Characters`"}} end

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