Program to check if a String in Java contains only whitespaces


The whitespaces of JAVA represent either an empty string or an escape sequence character. In Java, we have some built-in functions such as isEmpty(), lenThe whitespaces of JAVA represent either an empty string or an escape sequence character. In Java, we have some built-in functions such as isEmpty(), length(), isWhitespace(), and, charAt() that will be used to check if a String in Java contains only whitespaces. The whitespaces in the program are only used for formatting purposes such as \n(new line), \t(horizontal tab), \v(vertical tab), etc.

Syntax

The following syntax is used in the examples-

isEmpty()

The built-in function isEmpty() defines whether the given string is empty or not.

length()

The length() is a built-in function in Python that returns the length of the given string.

isWhitespace()

The isWhitespace() is an in-built function in Python that returns true if the character value passes to a parameter that satisfies as whitespace.

charAt()

The charAt() is an in-built function in Python that returns the character of the specific index of a given string.

Algorithm

The following steps are-

Step 1: We will start the program by defining the method check_whitespaces() of boolean data type that accept the str as a parameter of type String.

Step 2: Then use the first if-statement to check whether the string is null or empty by using the built-in function isEmpty(), and operators such as logical OR and isEmpty().

Step 3: Next, it will use the for loop where variable i iterates through the length of all the given strings such as str1, str2, str3, str4, and str5. Then use the built-in function charAt() that accepts variable i to separate each character of the string and store it in the variable c.

Step 4: Then use the second if-statement to set the condition to check whether the character is satisfied for whitespaces or not using the built-in function isWhitespaces() and return false if the condition does not satisfy otherwise true.

Step 5: Now it will use the main function and start initializing the different strings to set some values such as escape characters(\t and \n), text string(“Hello”), and empty spaces(“ ”) to check for whitespaces.

Step 6: Finally, it will print the result with the help of calling method named check_whitespaces().

Example

In the following example, we will store some values such as escape sequence characters, empty spaces, and string text to their respective variables and check for the existence of whitespace.

public class whitespace_in_java {
    public static boolean check_whitespace(String str) {
// Check whether the string is null or empty
        if (str == null || str.isEmpty()) {
            return false;
        }
// The for loop iterate through each character of the string
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
// Check whether the character does not satisfy for whitespace
            if (!Character.isWhitespace(c)) {
                return false;
            }
        }
// Return true if the character satisfies for whitespace
        return true;
    }
    public static void main(String[] args) {
// Initializing different strings
        String str_1 = " \t   ";
        String str_2 = "  Hello  ";
        String str_3 = " ";
        String str_4 = "World";
        String str_5 = " \n ";
// Printing of all the given string
        System.out.println("The following string results are:-");
        System.out.println(check_whitespace(str_1)); 
        System.out.println(check_whitespace(str_2)); 
        System.out.println(check_whitespace(str_3)); 
        System.out.println(check_whitespace(str_4));
        System.out.println(check_whitespace(str_5));
    }
}

Output

The following string results are:-
true
false
true
false
true

Conclusion

The whitespaces are normally used in every programming language to set the format of the code. The whitespaces enhance code readability for every user and get a better understanding of the program. In Java, there is a total of 8 escape sequences that represent the valis character literals.

Updated on: 17-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements