Determining Space Characters in Java

JavaJavaBeginner
Practice Now

Introduction

The isspacechar() method of the Character class in Java is used to determine whether the given character is a space character or not. It checks if the character belongs to the U+0020 'SPACE', U+0085 'NEXT LINE', U+000D 'CARRIAGE RETURN', U+000A 'LINE FEED', U+0009 'CHARACTER TABULATION' or U+000B 'LINE TABULATION' space character groups.


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/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/break_continue("`Break/Continue`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/class_methods -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/modifiers -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/wrapper_classes -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/identifier -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/arrays -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/break_continue -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/data_types -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/for_loop -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/if_else -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/operators -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/output -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/strings -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/variables -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/string_methods -.-> lab-117547{{"`Determining Space Characters in Java`"}} java/system_methods -.-> lab-117547{{"`Determining Space Characters in Java`"}} end

Create the main method

In Java, the main method is the entry point of the program. All Java programs start executing from the main method.

Create the main method with the following code:

public static void main(String[] args) {

}

Initialize a string with a value

Create a string object named text and assign it a value of "Hello World!" using the following code:

String text = "Hello World!";

Check if the string contains space characters

Use the isspacechar() method in Character class to check if the string contains any space characters.

for (int i = 0; i < text.length(); i++) {
  boolean isSpaceChar = Character.isSpaceChar(text.charAt(i));
  if (isSpaceChar) {
    System.out.println("Space character found!");
    break;
  }
}

The for loop iterates over all the characters in the string text. The Charater.isSpaceChar() method checks if the current character is a space character or not. If true, the loop is terminated and a message "Space character found!" is displayed in the console.

Test the code

Compile the code using the following command:

javac CharacterSpace.java

Execute the code using the following command:

java CharacterSpace

The output should be:

Space character found!

This means that the code has successfully detected a space character in the text string.

Summary

In this lab, you learned how to use the isspacechar() method of the Character class to determine if a given character is a space character or not. You also learned how to use a for loop to iterate over a string and check all characters in the string for space characters.

Other Java Tutorials you may like