Check if multiple characters are letters or digits
Now, define multiple char
variables and initialize them with different characters. Then, use a loop to iterate over all the characters and use the isLetterOrDigit(char ch)
method to check if each character is a letter or a digit. Print the result for each character to the console.
public class LetterOrDigit {
public static void main(String[] args) {
char ch1 = 'A';
char ch2 = '5';
char ch3 = ':';
char ch4 = 'd';
char ch5 = '%';
char[] chars = {ch1, ch2, ch3, ch4, ch5};
for (char ch : chars) {
boolean letterOrDigit = Character.isLetterOrDigit(ch);
System.out.println("Is " + ch + " a letter or a digit? " + letterOrDigit);
}
}
}
Save the file and run the same command in the terminal to compile and run the file.