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.

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