check if a string is alphabetic java code example

Example 1: how to check if a string is in alphabetical order in java

public static boolean checkAlphabetic(String input) {
    for (int i = 0; i != input.length(); ++i) {
        if (!Character.isLetter(input.charAt(i))) {
            return false;
        }
    }

    return true;
}

Example 2: check element is alphabet java

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

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

    // checks 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.");
    }
  }
}

Tags:

Java Example