Java Character IsLetter Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the Java isLetter(int codePoint) method, which is used to check whether the specified Unicode codepoint character is a letter or not.

Create a Java file

First, we need to create a Java file CharacterIsLetter.java in the ~/project directory.

cd ~/project
touch CharacterIsLetter.java

Write the code

Now, open the CharacterIsLetter.java file with your favorite text editor and paste the following code block.

import java.util.Scanner;

public class CharacterIsLetter {
    public static void main(String[] args) {
        int cp1 = 48;
        int cp2 = 61;
        int cp3 = 119;
        int cp4 = 90;
        int cp5 = 1232;
        boolean b1 = Character.isLetter(cp1);
        boolean b2 = Character.isLetter(cp2);
        boolean b3 = Character.isLetter(cp3);
        boolean b4 = Character.isLetter(cp4);
        boolean b5 = Character.isLetter(cp5);
        System.out.println((char) cp1 + " is a letter?: " + b1);
        System.out.println((char) cp2 + " is a letter?: " + b2);
        System.out.println((char) cp3 + " is a letter?: " + b3);
        System.out.println((char) cp4 + " is a letter?: " + b4);
        System.out.println((char) cp5 + " is a letter?: " + b5);
        Scanner scanner = new Scanner(System.in);
        System.out.print("\nEnter the Unicode character: ");
        int cp = scanner.nextInt();
        boolean isLetter = Character.isLetter(cp);
        System.out.println((char) cp + " is a letter?: " + isLetter);
    }
}

We use Scanner to allow users to enter their own Unicode characters for testing purposes. The code first checks whether some specific characters are letters or not and then prompts the user to enter a Unicode character. Finally, the code checks whether the entered Unicode character is a letter or not.

Compile and run the code

In the terminal, compile the CharacterIsLetter.java file using the following command:

javac CharacterIsLetter.java

Now, run the program using the following command:

java CharacterIsLetter

You should see an output similar to the following:

0 is a letter?: false
= is a letter?: false
w is a letter?: true
Z is a letter?: true
เค  is a letter?: true

Enter the Unicode character: 48
0 is a letter?: false

Now, try entering different Unicode characters to check whether they are letters or not.

Edit the code

Now, let's edit the code to test a different Unicode character. Change the value of cp to the Unicode codepoint of any character of your choice.

Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter the Unicode character: ");
int cp = scanner.nextInt();
boolean isLetter = Character.isLetter(cp);
System.out.println((char) cp + " is a letter?: " + isLetter);

Save the file and re-run the program using the following command:

java CharacterIsLetter

Now, enter your desired Unicode character and see whether it is a letter or not.

Test a non-letter character

Let's now test the isLetter method by passing a non-letter Unicode character to it.

Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter the Unicode character: ");
int cp = scanner.nextInt();
boolean isLetter = Character.isLetter(cp);
System.out.println((char) cp + " is a letter?: " + isLetter);

Save the file and re-run the program using the following command:

java CharacterIsLetter

Now, enter a non-letter Unicode character such as # and see whether the output is as expected.

Summary

In this lab, we learned about the Java isLetter(int codePoint) method, which is used to check whether a specified Unicode codepoint character is a letter or not. We also learned how to write and run a Java code that uses this method to check whether a given Unicode character is a letter or not.

Other Java Tutorials you may like