Identifying Ignorable Unicode Characters

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the Java isIdentifierIgnorable(int codePoint) method, which is used to check whether a character can be considered as an ignorable character or a Unicode identifier in Java. In this lab, you will be given step-by-step instructions to create a Java program that demonstrates the use of this 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/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/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/classes_objects -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/class_methods -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/exceptions -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/modifiers -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/oop -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/packages_api -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/user_input -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/wrapper_classes -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/identifier -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/arrays -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/data_types -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/if_else -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/operators -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/output -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/strings -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/variables -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} java/system_methods -.-> lab-117519{{"`Identifying Ignorable Unicode Characters`"}} end

Create a new Java file

In the terminal of Ubuntu, create a new Java file named CharacterDemo.java in the ~/project directory using the following command:

touch ~/project/CharacterDemo.java

Write code to take user input

In the CharacterDemo.java file, write the following code to take user input in the form of a codepoint value:

import java.util.Scanner;

public class CharacterDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Unicode codepoint: ");
        int codepoint = sc.nextInt();
    }
}

The above code will import the Scanner class from the java.util package and use it to take user input in the form of a codepoint value. The entered value will be stored in the codepoint variable.

Check if entered character is ignorable or not

In the CharacterDemo.java file, add the following code to check if the entered character is ignorable or not:

if(Character.isIdentifierIgnorable(codepoint)){
    System.out.println("The entered character is ignorable.");
} else {
    System.out.println("The entered character is not ignorable.");
}

The above code checks if the entered character is ignorable or not using the isIdentifierIgnorable(int codePoint) method. If the entered character is ignorable, it will print "The entered character is ignorable." on the console. Otherwise, it will print "The entered character is not ignorable."

Compile and run the program

To compile the CharacterDemo.java file, run the following command in the terminal:

javac CharacterDemo.java

After successful compilation, run the following command to execute the program:

java CharacterDemo

You will now be prompted to enter a Unicode codepoint value. Enter a codepoint value and hit enter.

If the entered value is an ignorable character, you will see the message "The entered character is ignorable." on the console. Otherwise, you will see the message "The entered character is not ignorable."

Modify the code to handle exceptions

In case of an invalid input, like if the user enters a non-integer value, the program should handle the exception and print an error message to the console. Update the code to include a try-catch block to handle invalid input as follows:

try {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a Unicode codepoint: ");
    int codepoint = sc.nextInt();

    if(Character.isIdentifierIgnorable(codepoint)){
        System.out.println("The entered character is ignorable.");
    } else {
        System.out.println("The entered character is not ignorable.");
    }
} catch (Exception e){
    System.out.println("Invalid input! Please enter a valid codepoint value.");
}

Compile and run the modified program

Compile the modified CharacterDemo.java file using the command:

javac CharacterDemo.java

Run the program using the command:

java CharacterDemo

Test the program by entering the following inputs in the terminal:

  • 0xEFFF
  • 0x001F
  • 0x02a6
  • abcd

The expected output for the above inputs is:

  • 0xEFFF: The entered character is not ignorable.
  • 0x001F: The entered character is ignorable.
  • 0x02a6: The entered character is not ignorable.
  • abcd: Invalid input! Please enter a valid codepoint value.

Summary

Congratulations, you have successfully learned how to use the isIdentifierIgnorable(int codePoint) method in Java to check if a character is ignorable or not. In this lab, you created a Java program that takes a user input in the form of a codepoint value and used the isIdentifierIgnorable(int codePoint) method to check if the entered character is ignorable or not. You also learned how to handle exceptions in case of invalid input. Use the information learned in this lab to implement your own programs that use the isIdentifierIgnorable(int codePoint) method.

Other Java Tutorials you may like