Java Character isLetterOrDigit() Example

Java Character API comes with many methods to test whether a character is a digit or letter etc. This type of testing is useful in situations like constraints on passwords, employee id etc.

There exists three methods in Character class

  1. to check a character is letter with isLetter(char ch) method
  2. to check a character is digit with isDigit(char ch) method
  3. to check a character is either a letter or a digit with isLetterOrDigit(char ch) method

Now we see the third method – isLetterOrDigit(char ch)

Following is the method signature as defined in java.lang.Character class.

  • public static boolean isLetterOrDigit(char ch): Checks the specified character ch is a letter or digit. The method returns true if the char ch is either a letter or digit, else false.

Following example illustrates. Just for interest, special characters are also checked.

public class CharacterFunctionDemo
  public static void main(String args[])
   char ch1 = 'A';
   char ch2 = '5';                      

   boolean b1 = Character.isLetterOrDigit(ch1);
   boolean b2 = Character.isLetterOrDigit(ch2);

   System.out.println(ch1 + " is letter or digit: " + b1);     
   System.out.println(ch2 + " is letter or digit: " + b2);     

   System.out.println("\n+ is letter or digit: " + Character.isLetterOrDigit('+'));
   System.out.println("$ is letter or digit: " + Character.isLetterOrDigit('$'));

Java Character isLetterOrDigit() Example

Leave a Comment

Your email address will not be published.