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.