Check whether the entered value is whitespace or not in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 14:47:27
Advertisements