Check whether the entered value is whitespace or not in Java


To check whether the entered value is whitespace or not in Java, use the Character.isWhitespace() method.

We have a value to be checked.

char val = ' ';

Now let us use the Character.isWhitespace() method.

if (Character.isWhitespace(val)) {
   System.out.println("Value is a Whitespace!");
} else {
   System.out.println("Value is not a Whitespace");
}

Let us see the complete example now to check whether the entered value is whitespace or not in Java.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      char val =' ';
      System.out.println("Value: "+val);
      if (Character.isWhitespace(val)) {
         System.out.println("Value is a Whitespace!");
      } else {
         System.out.println("Value is not a Whitespace");
      }
   }
}

Output

Value:
Value is a Whitespace!

Let us see another example.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      char val ='L';
      System.out.println("Value: "+val);
      if (Character.isWhitespace(val)) {
         System.out.println("Value is a Whitespace!");
      }else {
         System.out.println("Value is not a Whitespace");
      }
   }
}

Output

Value: L
Value is not a Whitespace

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements