Java Character isSurrogate Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the isSurrogate() method of the Character class in Java. This method is used to check whether a given character is a Unicode surrogate code unit or not. The isSurrogate() method is a static method, which means that it can be called directly on the Character class without creating an instance of the class.


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/recursion("`Recursion`") java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") 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/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") 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/recursion -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/scope -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/classes_objects -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/class_methods -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/modifiers -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/oop -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/packages_api -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/user_input -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/wrapper_classes -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/identifier -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/arrays -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/data_types -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/for_loop -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/if_else -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/operators -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/output -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/strings -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/variables -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/string_methods -.-> lab-117551{{"`Java Character isSurrogate Method`"}} java/system_methods -.-> lab-117551{{"`Java Character isSurrogate Method`"}} end

Create a new Java file

Create a new Java file named IsSurrogateDemo.java in your ~/project directory.

touch ~/project/IsSurrogateDemo.java

Write the Java code

Copy and paste the following code into the IsSurrogateDemo.java file:

import java.util.Scanner;

public class IsSurrogateDemo {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a character: ");
      char ch = sc.nextLine().charAt(0);
      boolean isSurrogateChar = Character.isSurrogate(ch);
      if (isSurrogateChar) {
         System.out.println(ch + " is a surrogate code unit.");
      } else {
         System.out.println(ch + " is not a surrogate code unit.");
      }
   }
}

Compile and run the program

Open a terminal and navigate to the ~/project folder. Compile the IsSurrogateDemo.java file using the following command:

javac IsSurrogateDemo.java

Once the program is compiled without errors, run the program using the following command:

java IsSurrogateDemo

Optional): Test with a string of characters

You can modify the Java code to test a string of characters instead of a single character. Here's how you can modify the code:

import java.util.Scanner;

public class IsSurrogateDemo {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a string: ");
      String input = sc.nextLine();
      for (int i = 0; i < input.length(); i++) {
         char ch = input.charAt(i);
         boolean isSurrogateChar = Character.isSurrogate(ch);
         if (isSurrogateChar) {
            System.out.println(ch + " is a surrogate code unit.");
         } else {
            System.out.println(ch + " is not a surrogate code unit.");
         }
      }
   }
}

Compile and run the modified program using the same commands as before (javac IsSurrogateDemo.java and java IsSurrogateDemo).

Modify the program to use command-line arguments

You can modify the Java code to accept the input character or string from the command line arguments instead of prompting the user to enter the input. Here's how you can modify the code:

public class IsSurrogateDemo {
   public static void main(String[] args) {
      if (args.length == 0) {
         System.out.println("Please enter a character or string as command-line argument.");
         return;
      }

      for (String arg : args) {
         for (int i = 0; i < arg.length(); i++) {
            char ch = arg.charAt(i);
            boolean isSurrogateChar = Character.isSurrogate(ch);
            if (isSurrogateChar) {
               System.out.println(ch + " is a surrogate code unit.");
            } else {
               System.out.println(ch + " is not a surrogate code unit.");
            }
         }
      }
   }
}

Compile and run the modified program using the following commands (replace "e" with any character or string of your choice):

javac IsSurrogateDemo.java
java IsSurrogateDemo e

Summary

Congratulations! You have learned how to use the isSurrogate() method of the Character class in Java. This method is used to check whether a given character is a Unicode surrogate code unit or not. You have also learned how to accept input from the user and command-line arguments, and how to modify the program accordingly.

Other Java Tutorials you may like