Java Character isJavaIdentifierPart Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the Character class provides a lot of useful methods that help us to work with characters. One of these methods is isJavaIdentifierPart(int codePoint), which checks whether the specified Unicode codepoint character is a part of a Java identifier or not.

Set Up

First, create a file named Main.java in the ~/project directory using the following command:

touch ~/project/Main.java

Then, open the file using a text editor of your choice:

touch ~/project/Main.java

Check if a character is a part of a Java identifier

In this step, you will use the isJavaIdentifierPart method to check whether a given character is a part of a Java identifier or not.

Add the following code to the Main.java file:

public class Main {
    public static void main(String[] args) {
        int codePoint = 65;
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(codePoint);

        if(isJavaIdentifierPart) {
            System.out.println("The character is a part of a Java identifier.");
        } else {
            System.out.println("The character is not a part of a Java identifier.");
        }
    }
}

The above code is checking whether the character with Unicode code point 65 is a part of a Java identifier or not. In this case, 65 corresponds to the uppercase letter 'A'.

To run the code, use the following command:

javac Main.java && java Main

User Input

In this step, you will modify the code to accept user input for the Unicode code point.

Add the following code to replace the contents of the main method in the Main.java file:

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

    System.out.print("Enter a Unicode code point: ");
    int codePoint = scanner.nextInt();

    boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(codePoint);

    if(isJavaIdentifierPart) {
        System.out.println("The character is a part of a Java identifier.");
    } else {
        System.out.println("The character is not a part of a Java identifier.");
    }

    scanner.close();
}

The above code prompts the user to enter a Unicode code point and then checks whether the corresponding character is a part of a Java identifier or not.

To run the code, use the following command:

javac Main.java && java Main

Test with different Unicode code points

In this step, you will test the code with different Unicode code points to check whether the corresponding characters are a part of a Java identifier or not.

Run the Main.java file and enter different Unicode code points to test the isJavaIdentifierPart method.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Check if a character is a part of a Java identifier using a string

In this step, you will modify the code to check whether a character in a string is a part of a Java identifier or not.

Add the following code to replace the contents of the main method in the Main.java file:

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

    System.out.print("Enter a string: ");
    String line = scanner.nextLine();

    for (int i = 0; i < line.length(); i++) {
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(line.codePointAt(i));
        char c = line.charAt(i);

        if(isJavaIdentifierPart) {
            System.out.println("The character '" + c + "' at position " + i + " is a part of a Java identifier.");
        } else {
            System.out.println("The character '" + c + "' at position " + i + " is not a part of a Java identifier.");
        }
    }

    scanner.close();
}

The above code prompts the user to enter a string and then checks whether each character in the string is a part of a Java identifier or not.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Ignore the case sensitivity

In this step, you will modify the code to ignore the case sensitivity when checking whether a character is a part of a Java identifier or not.

Add the following code to replace the contents of the main method in the Main.java file:

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

    System.out.print("Enter a string: ");
    String line = scanner.nextLine().toLowerCase();

    for (int i = 0; i < line.length(); i++) {
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(line.codePointAt(i));
        char c = line.charAt(i);

        if(isJavaIdentifierPart) {
            System.out.println("The character '" + c + "' at position " + i + " is a part of a Java identifier.");
        } else {
            System.out.println("The character '" + c + "' at position " + i + " is not a part of a Java identifier.");
        }
    }

    scanner.close();
}

The above code converts the user input string to lowercase and then checks whether each character in the string is a part of a Java identifier or not, ignoring the case sensitivity.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Summary

In this lab, you learned how to use the isJavaIdentifierPart method to check whether a given character is a part of a Java identifier or not. You also learned how to accept user input and how to check whether each character in a string is a part of a Java identifier or not. Finally, you saw how to ignore the case sensitivity when checking whether a character is a part of a Java identifier or not.

Other Java Tutorials you may like