Introduction
The isWhitespace(int codePoint) method in Java is used to check whether the specified Unicode codepoint character is whitespace or not. This method is a part of the Character class and is used to determine if a given Unicode character is a white space or not.
Create a Java file
Create a new Java file named IsWhiteSpace.java in the ~/project directory with the following command:
touch ~/project/IsWhiteSpace.java
Add code to the file
Add the following code to the IsWhiteSpace.java file:
public class IsWhiteSpace {
public static void main(String[] args) {
int cp1 = 10;
int cp2 = 60;
int cp3 = 119;
int cp4 = 11;
int cp5 = 1232;
boolean b1 = Character.isWhitespace(cp1);
boolean b2 = Character.isWhitespace(cp2);
boolean b3 = Character.isWhitespace(cp3);
boolean b4 = Character.isWhitespace(cp4);
boolean b5 = Character.isWhitespace(cp5);
System.out.println((char)cp1 + " is a Java Whitespace??: " + b1);
System.out.println((char)cp2 + " is a Java Whitespace??: " + b2);
System.out.println((char)cp3 + " is a Java Whitespace??: " + b3);
System.out.println((char)cp4 + " is a Java Whitespace??: " + b4);
System.out.println((char)cp5 + " is a Java Whitespace??: " + b5);
}
}
The above code creates a IsWhiteSpace class with the main method and then checks whether the specified codepoint character is whitespace or not and prints the result accordingly.
Compile and run the code
Compile the code using the following command:
javac IsWhiteSpace.java
Now run the code using the following command:
java IsWhiteSpace
You should see the following output:
is a Java Whitespace??: true
< is a Java Whitespace??: false
w is a Java Whitespace??: false
is a Java Whitespace??: true
? is a Java Whitespace??: false
User input example
Now let's write a user input example. Update the existing IsWhiteSpace.java file with the following code:
import java.util.Scanner;
public class IsWhiteSpace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a Unicode character: ");
int input = scanner.next().charAt(0);
boolean isWhitespace = Character.isWhitespace(input);
System.out.println(input + " is a Java whitespace character?: " + isWhitespace);
}
}
The above code takes user input, checks whether the input is a Java whitespace character, and prints the result accordingly.
Compile and run the code again
Compile the code using the following command:
javac IsWhiteSpace.java
Now run the code using the following command:
java IsWhiteSpace
You should see the following output:
Enter a Unicode character:
5
53 is a Java whitespace character?: false
Now enter any character to test if it is a Java whitespace character or not.
Summary
In this lab, you have learned how to use the Java isWhitespace(int codePoint) method which is a part of Character class. You also learned how to determine whether a specified codepoint character is whitespace or not in Java.



