Java Character isWhitespace Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/classes_objects -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/class_methods -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/modifiers -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/oop -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/packages_api -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/user_input -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/wrapper_classes -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/identifier -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/arrays -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/data_types -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/operators -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/output -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/strings -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/type_casting -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/variables -.-> lab-117572{{"`Java Character isWhitespace Method`"}} java/system_methods -.-> lab-117572{{"`Java Character isWhitespace Method`"}} end

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.

Other Java Tutorials you may like