Check whether the entered value is a letter or not in Java

Java 8Object Oriented ProgrammingProgramming

To check whether the entered value is a letter or not in Java, use the Character.isLetter() method.

We have a value to be checked.

char val = 'D';

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

if (Character.isLetter(val)) {
   System.out.println("Character is in Lowercase!");
}else {
   System.out.println("Character is in Uppercase!");
}

Let us see the complete example now to check for letter.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      System.out.println("Checking whether the given value is a Letter or not...");
      char val = 'D';
      System.out.println("Value: "+val);
      if (Character.isLetter(val)) {
         System.out.println("Value is a letter!");
      }else {
         System.out.println("Value is a number!");
      }
   }
}

Output

Checking whether the given value is a Letter or not...
Value: D
Value is a letter!

Let us see another example.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      System.out.println("Checking whether the given value is a Letter or not...");
      int val = 20;
      System.out.println("Value: "+val);
      if (Character.isLetter(val)) {
         System.out.println("Value is a letter!");
      }else {
         System.out.println("Value is not a letter!");
      }
   }
}

Output

Checking whether the given value is a Letter or not...
Value: 20
Value is not a letter!
raja
Published on 13-Nov-2018 14:40:10
Advertisements