Syntax error Check whether the entered value is whitespace or not in Java

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
Updated on: 2020-06-26T12:14:28+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements