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.

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