Identifying Ignorable Java Characters

JavaJavaBeginner
Practice Now

Introduction

The isIdentifierIgnorable(char ch) method is a part of the Character class in Java. This method is used to check whether a specific character can be considered as an ignorable character in Java or a Unicode identifier or not.

Declare the main method

Declare the main method, which is the entry point of the Java program, as shown below:

public static void main(String[] args) {
    // your code here
}

Create some variables

Create some variables with different characters, as shown below:

char ch1 = '\u0000'; // ASCII null character
char ch2 = '\u001F'; // ASCII unit separator character
char ch3 = '\u007F'; // ASCII delete character
char ch4 = 'a';      // a normal character

Check if characters are identifier ignorable

Using the Character.isIdentifierIgnorable(char ch) method, print whether each character is an ignorable character in Java or Unicode identifier.

System.out.println("Is " + ch1 + " ignorable? " + Character.isIdentifierIgnorable(ch1));
System.out.println("Is " + ch2 + " ignorable? " + Character.isIdentifierIgnorable(ch2));
System.out.println("Is " + ch3 + " ignorable? " + Character.isIdentifierIgnorable(ch3));
System.out.println("Is " + ch4 + " ignorable? " + Character.isIdentifierIgnorable(ch4));

Here is the complete code for your reference:

public class IdentifierIgnorable {
    public static void main(String[] args) {
        char ch1 = '\u0000'; // ASCII null character
        char ch2 = '\u001F'; // ASCII unit separator character
        char ch3 = '\u007F'; // ASCII delete character
        char ch4 = 'a';      // a normal character

        System.out.println("Is " + ch1 + " ignorable? " + Character.isIdentifierIgnorable(ch1));
        System.out.println("Is " + ch2 + " ignorable? " + Character.isIdentifierIgnorable(ch2));
        System.out.println("Is " + ch3 + " ignorable? " + Character.isIdentifierIgnorable(ch3));
        System.out.println("Is " + ch4 + " ignorable? " + Character.isIdentifierIgnorable(ch4));
    }
}

Compile and run the Java program

Compile the Java program using the following command in the terminal:

javac ~/project/IdentifierIgnorable.java

Run the Java program using the following command:

java IdentifierIgnorable

Test the program

The program will output the result of whether each character is an ignorable character in Java or Unicode identifier.

Is ignorable? true
Is ignorable? true
Is ignorable? true
Is a ignorable? false

You can test the program with different characters and check their output.

Modify the program

Try modifying the program to take input from the user and check if the character is an ignorable character in Java or a Unicode identifier. Here is the modified code:

import java.util.Scanner;

public class IdentifierIgnorable {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = input.next().charAt(0);

        System.out.println("Is " + ch + " ignorable? " + Character.isIdentifierIgnorable(ch));
    }
}

Compile and run the modified program

Compile the modified Java program using the following command in the terminal:

javac ~/project/IdentifierIgnorable.java

Run the modified Java program using the following command:

java IdentifierIgnorable

Test the modified program

The program will ask for a character as input and output whether it is an ignorable character in Java or Unicode identifier.

Enter a character:
$
Is $ ignorable? false

Summary

In this lab, you have learned:

  • The Java isIdentifierIgnorable(char ch) method is used to check whether a character can be considered as an ignorable character in Java or a Unicode identifier.
  • ISO control characters that are not whitespace, and all characters that have the FORMAT general category value, are considered as ignorable characters or Unicode identifier.
  • How to use this method to check whether a character is an ignorable character in Java or Unicode identifier.

Other Java Tutorials you may like