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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/classes_objects -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/class_methods -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/exceptions -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/modifiers -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/oop -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/packages_api -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/user_input -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/wrapper_classes -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/identifier -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/arrays -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/data_types -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/operators -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/output -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/strings -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/variables -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/string_methods -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} java/system_methods -.-> lab-117539{{"`Java Character isLowSurrogate Method`"}} end

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