LAST UPDATED: NOVEMBER 24, 2020
Java Character isLetterOrDigit(char ch) Method
Java isLetterOrDigit(char ch)
method is a part of Character
class. This method is used to check whether the specified character is a letter or a digit.
A character can be considered as a letter or a digit if either Character.isLetter(char ch)
or Character.isDigit(char ch)
returns true
for the character.
Syntax:
public static boolean isLetterOrDigit(char ch)
Parameters:
The parameter passed is the character to be checked for digit or letter.
Returns:
Returns the boolean value true
if the specified character is a digit or a letter else return false
.
Example 1:
Here, the characters are checked whether they are a digit or a letter or not.
public class StudyTonight
{
public static void main(String[] args)
{
char ch1 = ':';
char ch2 = 'D';
char ch3 = '8';
char ch4 = 'w';
char ch5 = '%';
boolean b1 = Character.isLetterOrDigit(ch1);
boolean b2 = Character.isLetterOrDigit(ch2);
boolean b3 = Character.isLetterOrDigit(ch3);
boolean b4 = Character.isLetterOrDigit(ch4);
boolean b5 = Character.isLetterOrDigit(ch5);
System.out.println(ch1 +" is a letter or digit??: "+b1);
System.out.println(ch2 +" is a letter or digit??: "+b2);
System.out.println(ch3 +" is a letter or digit??: "+b3);
System.out.println(ch4 +" is a letter or digit?? : "+b4);
System.out.println(ch5 +" is a letter or digit??: "+b5);
}
}
: is a letter or digit??: false
D is a letter or digit??: true
8 is a letter or digit??: true
w is a letter or digit?? : true
% is a letter or digit??: false
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the character: ");
Scanner sc = new Scanner(System.in);
char ch = sc.next().charAt(0);
boolean b = Character.isLetterOrDigit(ch);
System.out.println(ch + " is a Java Letter or digit?: "+b);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
Enter the character: 2
2 is a Java Letter or digit?: true
***************************************
Enter the character: 2
2 is a Java Letter or digit?: true
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.