Java Program to Check Whether a Character is Alphabet or Not

Example 1: Java Program to Check Alphabet using if else

public class CheckAlphabet {

    public static void main(String[] args) {

//declare a variable
        char c = 'E';

//check if c is an alphabet
        if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            System.out.println(c + " is an alphabet.");
        else
            System.out.println(c + " is not an alphabet.");
    }
}

Output

E is an alphabet.

Example 2: Java Program to Check Alphabet using ternary operator

public class CheckAlphabet {

    public static void main(String[] args) {

//declare a variable
        char c = '*';

//check if c is an alphabet or not        
        String output = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
                ? c + " is an alphabet."
                : c + " is not an alphabet.";
        
        System.out.println(output);
    }
}

Output

* is not an alphabet.

Example 3: Java Program to Check Alphabet using isAlphabetic() Method

class CheckAlphabet {
  public static void main(String[] args) {

    // declare a variable
    char c = 'a';

    // check if c is an alphabet
    if (Character.isAlphabetic(c)) {
      System.out.println(c + " is an alphabet.");
    }
    else {
      System.out.println(c + " is not an alphabet.");
    }
  }
}

Output

a is an alphabet.

Leave a Reply