Exploring Java Character Space Detection

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to use the isSpaceChar(char ch) method of the Character class in Java to determine whether a given character is a Unicode space character or not. We will write Java code with user-defined examples to demonstrate the usage of this method.


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/exceptions("`Exceptions`") 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/comments("`Comments`") 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/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/classes_objects -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/class_methods -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/exceptions -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/modifiers -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/oop -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/packages_api -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/user_input -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/wrapper_classes -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/identifier -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/arrays -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/comments -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/data_types -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/operators -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/output -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/strings -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/variables -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} java/system_methods -.-> lab-117545{{"`Exploring Java Character Space Detection`"}} end

Setting up the project

First, let's create a Java project in the ~/project directory, and create a new Java code file called SpaceCharDemo.java using the following command:

mkdir ~/project
cd ~/project
touch SpaceCharDemo.java

Understanding the isSpaceChar(char ch) method

The isSpaceChar(char ch) method is a built-in method of the Java Character class. It can be used to check whether a given character is a Unicode space character or not. This method returns true if the character's general category type is any of the following types:

  • SPACE_SEPARATOR
  • LINE_SEPARATOR
  • PARAGRAPH_SEPARATOR

Let's look at the syntax of this method:

public static boolean isSpaceChar(char ch)

Demonstrating the isSpaceChar(char ch) method

Let's implement a Java program that demonstrates the usage of the isSpaceChar(char ch) method using user-defined examples. Copy the following code to your SpaceCharDemo.java file:

import java.util.Scanner;

public class SpaceCharDemo {
    public static void main(String[] args) {
        // Example 1
        char ch1 = 'h';
        char ch2 = '\u2028';
        char ch3 = '\u2078';
        char ch4 = '0';
        char ch5 = ' ';

        boolean b1 = Character.isSpaceChar(ch1);
        boolean b2 = Character.isSpaceChar(ch2);
        boolean b3 = Character.isSpaceChar(ch3);
        boolean b4 = Character.isSpaceChar(ch4);
        boolean b5 = Character.isSpaceChar(ch5);

        System.out.println(ch1 + " is a space character? " + b1);
        System.out.println(ch2 + " is a space character? " + b2);
        System.out.println(ch3 + " is a space character? " + b3);
        System.out.println(ch4 + " is a space character? " + b4);
        System.out.println(ch5 + " is a space character? " + b5);

        // Example 2
        try {
            System.out.print("Enter a character: ");
            Scanner sc = new Scanner(System.in);
            char ch = sc.next().charAt(0);
            boolean b = Character.isSpaceChar(ch);
            System.out.println(ch + " is a space character? " + b);
        } catch (Exception e) {
            System.out.println("Invalid Input!!");
        }
    }
}

In the above code, we have demonstrated the usage of the isSpaceChar(char ch) method using two examples.

In the first example, we have declared five characters of different types and using the isSpaceChar(char ch) method, we have identified whether the given characters are Unicode space characters or not. In the second example, we have taken input from the user and used the isSpaceChar(char ch) method to identify whether the given character is a space character or not.

Note that in the second example, we are using the Scanner class to take input from the user.

Compiling and running the code

Now let's use the following commands to compile and run the Java code we just created:

javac SpaceCharDemo.java
java SpaceCharDemo

Testing the program

When you run the above code, it will provide you with two examples to check whether the given characters are space characters or not. The output of the given examples will be like the following:

h is a space character? false
ā€‹ is a space character? true
āø is a space character? false
0 is a space character? false
  is a space character? true
Enter a character: $
$ is a space character? false

You can enter any character to check whether it is a space character or not.

Summary

In this lab, we have learned how to use the isSpaceChar(char ch) method of the Character class in Java to check whether a given character is a Unicode space character or not. We have also learned how to create a Java program with user-defined examples to demonstrate the usage of this method.

Other Java Tutorials you may like