Introduction
This lab will walk you through the usage of the Java lowSurrogate()
Method. You will learn what this method does, what are its parameters and return type, and how it can be used in Java programming.
This lab will walk you through the usage of the Java lowSurrogate()
Method. You will learn what this method does, what are its parameters and return type, and how it can be used in Java programming.
java.util.Scanner
and java.lang.Character
Import the java.util.Scanner
and java.lang.Character
packages at the top of your Java file, so that you can use their classes in your code.
import java.util.Scanner;
import java.lang.Character;
Create a main() method inside your class. The main() method is the entry point of your Java program, and all the code written inside this method will be executed when the program is run.
public static void main(String[] args) {
}
Take a Unicode character as an input from the user. You can use the Scanner
class to take user input. Initialize a new Scanner
object and use the nextInt()
method to take an integer input from the user.
Scanner input = new Scanner(System.in);
System.out.print("Enter a Unicode character: ");
int unicode = input.nextInt();
Use the lowSurrogate()
method of the Character
class to get the trailing surrogate of the Unicode character entered by the user. Pass the Unicode character to the lowSurrogate()
method as a parameter. The method will return the trailing surrogate as a char
type.
char trailingSurrogate = Character.lowSurrogate(unicode);
Display the Unicode character entered by the user and its corresponding trailing surrogate using the println()
method of the System.out
object.
System.out.println("The trailing surrogate of " + (char)unicode + " is " + trailingSurrogate);
Compile the Java program using the following command in the terminal:
javac LowSurrogateDemo.java
Run the program using the following command:
java LowSurrogateDemo
Enter a Unicode character when prompted and press Enter to get the trailing surrogate.
Enter a Unicode character: 128169
The trailing surrogate of ðĐ is ïŋ―
In this lab, you have learned how to use the Java lowSurrogate()
Method to get the trailing surrogate of a Unicode character. You have learned how to take user input, pass it to the method, display the output, compile and run the program. This method is useful when you need to manipulate Unicode strings in your Java programs.