Use Character.isWhitespace() Method
In this step, we will explore how to use the Character.isWhitespace()
method in Java. This method is part of the Character
class and is used to determine if a given character is a whitespace character. Whitespace characters include spaces, tabs, newlines, and carriage returns.
Understanding how to identify whitespace is useful in many programming tasks, such as parsing text, validating input, or formatting output.
Let's create a new Java file to experiment with this method.
-
Open the WebIDE if it's not already open.
-
In the File Explorer on the left, make sure you are in the ~/project
directory.
-
Right-click in the empty space within the ~/project
directory and select "New File".
-
Name the new file WhitespaceChecker.java
.
-
Open the WhitespaceChecker.java
file in the editor.
-
Copy and paste the following Java code into the editor:
public class WhitespaceChecker {
public static void main(String[] args) {
char space = ' ';
char tab = '\t';
char newline = '\n';
char letter = 'a';
char digit = '1';
System.out.println("Is '" + space + "' whitespace? " + Character.isWhitespace(space));
System.out.println("Is '" + tab + "' whitespace? " + Character.isWhitespace(tab));
System.out.println("Is '" + newline + "' whitespace? " + Character.isWhitespace(newline));
System.out.println("Is '" + letter + "' whitespace? " + Character.isWhitespace(letter));
System.out.println("Is '" + digit + "' whitespace? " + Character.isWhitespace(digit));
}
}
Let's look at the new parts of this code:
char space = ' ';
: This declares a variable named space
of type char
and assigns it the space character.
char tab = '\t';
: This declares a variable named tab
and assigns it the tab character. \t
is an escape sequence representing a tab.
char newline = '\n';
: This declares a variable named newline
and assigns it the newline character. \n
is an escape sequence representing a newline.
char letter = 'a';
: This declares a variable named letter
and assigns it the character 'a'.
char digit = '1';
: This declares a variable named digit
and assigns it the character '1'.
Character.isWhitespace(space)
: This is where we call the isWhitespace()
method. We pass the space
character to it, and it will return true
if the character is whitespace, and false
otherwise. We do the same for the other characters.
System.out.println(...)
: We use println
to print the result of the isWhitespace()
method call along with a descriptive message.
-
Save the WhitespaceChecker.java
file (Ctrl+S or Cmd+S).
Now that we have written the code, we need to compile and run it to see the output.
-
Open the Terminal at the bottom of the WebIDE. Make sure you are in the ~/project
directory.
-
Compile the Java program using the javac
command:
javac WhitespaceChecker.java
If there are no errors, this command will create a WhitespaceChecker.class
file in the ~/project
directory.
-
Run the compiled Java program using the java
command:
java WhitespaceChecker
You should see output similar to this:
Is ' ' whitespace? true
Is ' ' whitespace? true
Is '
' whitespace? true
Is 'a' whitespace? false
Is '1' whitespace? false
This output shows that the isWhitespace()
method correctly identified the space, tab, and newline characters as whitespace, and the letter and digit characters as non-whitespace.
You have successfully used the Character.isWhitespace()
method to check different characters. In the next step, we will test this method with more examples, including different types of whitespace.