Java Character isLowSurrogate Method

JavaJavaBeginner
Practice Now

Introduction

The isLowSurrogate() method in Java's Character class is used to check whether the specified character qualifies as a low-surrogate code unit or not. In this lab, you will learn how to use the isLowSurrogate() method and how it can be used in practical applications.

Setting Up the Code File

Open the terminal of your Ubuntu system, navigate to the ~/project directory, and create a new file named LowSurrogate.java using the nano editor.

cd ~/project
touch LowSurrogate.java

Writing the Java Code

In this step, you will write the code to showcase the use of the isLowSurrogate() method. Enter the following code in the LowSurrogate.java file.

import java.util.Scanner;

public class LowSurrogate {
      public static void main(String[] args) {
         try {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a character: ");
            char ch = sc.next().charAt(0);
            boolean isLow = Character.isLowSurrogate(ch);
            String result = isLow ? "is" : "is not";
            System.out.println(ch + " " + result + " a low-surrogate.");
         } catch(Exception e) {
            System.out.println("Invalid input!");
         }
      }
}

In the above code, we have imported the Scanner class to take input from the user. We take a character input from the user and store it in the ch variable. We then use the isLowSurrogate() method to check whether the input character is a low-surrogate or not. If the input character is a low-surrogate, then the isLow variable is set to true, and if it's not, then isLow is set to false.

We then use the ternary operator to set the result variable to "is" if isLow is true, and "is not" if isLow is false. Finally, we print the result message to the console.

Compiling and Running the Code

Save the changes and exit the nano editor by pressing Ctrl+X, followed by Y and Enter. Compile the Java code using the following command in the terminal.

javac LowSurrogate.java

Once the compilation is successful, run the code using the following command.

java LowSurrogate

The program will prompt you to enter a character. Type in a character and press Enter to see if it's a low-surrogate or not. You can run the code as many times as you want by executing the above command and providing a new input each time.

Testing the Code

Test the program by entering different characters as input and analyzing the output. Some sample inputs and outputs are shown below.

Sample Input 1:

Enter a character: A

Sample Output 1:

A is not a low-surrogate.

Sample Input 2:

Enter a character: 😊

Sample Output 2:

😊 is a low-surrogate.

Sample Input 3:

Enter a character: \ud835

Sample Output 3:

㌀ is not a low-surrogate.

Sample Input 4:

Enter a character: \

Sample Output 4:

Invalid input!

Modifying the Code (Optional)

Try modifying the code to check if a character is a high-surrogate instead of a low-surrogate. To do so, change the method call from Character.isLowSurrogate() to Character.isHighSurrogate() and modify the result message accordingly.

Enter a character: \ud835
\ud835 is a high-surrogate.

Summary

In this lab, you learned how to use the isLowSurrogate() method in Java's Character class to check if a character qualifies as a low-surrogate code unit. You also learned how to use the ternary operator to create a result message based on the output of the isLowSurrogate() method. Finally, you tested the code using different inputs and modified it to check for high-surrogate code units.

Other Java Tutorials you may like