Introduction
In this lab, you will learn how to determine if a character is a letter in Java using the Character.isLetter() method. You will explore how this method handles both uppercase and lowercase letters, and how it behaves with non-letter characters such as digits and symbols.
Through hands-on examples, you will write and execute Java code to test the Character.isLetter() method with various character types and observe the boolean results it returns. This will provide a practical understanding of how to effectively use this built-in Java function for character validation.
Use Character.isLetter() Method
In this step, we will learn how to use the Character.isLetter() method in Java. This method is part of the Character class and is used to determine if a given character is a letter.
The Character.isLetter() method takes a single character as input and returns a boolean value: true if the character is a letter (either uppercase or lowercase), and false otherwise.
Let's create a new Java file to experiment with this method.
Open the WebIDE. In the File Explorer on the left, make sure you are in the
~/projectdirectory.Right-click in the empty space in the File Explorer, select "New File", and name it
LetterCheck.java.Open the
LetterCheck.javafile in the editor.Copy and paste the following code into the editor:
public class LetterCheck { public static void main(String[] args) { char char1 = 'A'; char char2 = 'b'; char char3 = '5'; char char4 = '$'; boolean isChar1Letter = Character.isLetter(char1); boolean isChar2Letter = Character.isLetter(char2); boolean isChar3Letter = Character.isLetter(char3); boolean isChar4Letter = Character.isLetter(char4); System.out.println("Is '" + char1 + "' a letter? " + isChar1Letter); System.out.println("Is '" + char2 + "' a letter? " + isChar2Letter); System.out.println("Is '" + char3 + "' a letter? " + isChar3Letter); System.out.println("Is '" + char4 + "' a letter? " + isChar4Letter); } }In this code:
- We declare four
charvariables, each holding a different character. - We use
Character.isLetter()to check if each character is a letter and store the result in boolean variables. - We then print the results to the console.
- We declare four
Save the file by pressing
Ctrl + S(orCmd + Son Mac).Open the Terminal at the bottom of the WebIDE. Make sure you are in the
~/projectdirectory. You can use the commandcd ~/projectif needed.Compile the Java file using the
javaccommand:javac LetterCheck.javaIf there are no errors, this command will create a
LetterCheck.classfile in the same directory.Run the compiled Java program using the
javacommand:java LetterCheckYou should see output similar to this:
Is 'A' a letter? true Is 'b' a letter? true Is '5' a letter? false Is '$' a letter? false
This output confirms that Character.isLetter() correctly identified 'A' and 'b' as letters and '5' and '$' as non-letters.
Test with Uppercase and Lowercase
In the previous step, we saw that Character.isLetter() correctly identified both 'A' (uppercase) and 'b' (lowercase) as letters. This is because the method checks for both cases.
Let's modify our LetterCheck.java program to specifically test uppercase and lowercase characters and see how Character.isLetter() behaves.
Open the
LetterCheck.javafile in the WebIDE editor if it's not already open. It should be located in the~/projectdirectory.Modify the
mainmethod to include more examples of uppercase and lowercase letters. Replace the existingmainmethod content with the following:public static void main(String[] args) { char upperCaseA = 'A'; char lowerCaseZ = 'z'; char upperCaseM = 'M'; char lowerCaseC = 'c'; char digit5 = '5'; // Keep a non-letter for comparison boolean isUpperCaseALetter = Character.isLetter(upperCaseA); boolean isLowerCaseZLetter = Character.isLetter(lowerCaseZ); boolean isUpperCaseMLetter = Character.isLetter(upperCaseM); boolean isLowerCaseCLetter = Character.isLetter(lowerCaseC); boolean isDigit5Letter = Character.isLetter(digit5); System.out.println("Is '" + upperCaseA + "' a letter? " + isUpperCaseALetter); System.out.println("Is '" + lowerCaseZ + "' a letter? " + isLowerCaseZLetter); System.out.println("Is '" + upperCaseM + "' a letter? " + isUpperCaseMLetter); System.out.println("Is '" + lowerCaseC + "' a letter? " + isLowerCaseCLetter); System.out.println("Is '" + digit5 + "' a letter? " + isDigit5Letter); }We've added more examples covering different uppercase and lowercase letters.
Save the modified
LetterCheck.javafile (Ctrl + SorCmd + S).Open the Terminal (if it's not already open) and make sure you are in the
~/projectdirectory.Compile the updated Java file:
javac LetterCheck.javaAgain, if the compilation is successful, you won't see any output.
Run the compiled program:
java LetterCheckYou should see output similar to this:
Is 'A' a letter? true Is 'z' a letter? true Is 'M' a letter? true Is 'c' a letter? true Is '5' a letter? false
This confirms that Character.isLetter() correctly identifies all the uppercase and lowercase characters we tested as letters, while still identifying the digit '5' as not a letter. This demonstrates the method's ability to handle both cases of the English alphabet.
Handle Non-Letter Characters
In the previous steps, we've seen how Character.isLetter() works with both uppercase and lowercase letters. Now, let's explicitly test how it handles various non-letter characters, such as digits, symbols, and whitespace.
Open the
LetterCheck.javafile in the WebIDE editor. It should be in the~/projectdirectory.Modify the
mainmethod to include more examples of non-letter characters. Replace the existingmainmethod content with the following:public static void main(String[] args) { char digit0 = '0'; char symbolDollar = '$'; char space = ' '; char newLine = '\n'; // Newline character char comma = ','; char upperCaseA = 'A'; // Keep a letter for comparison boolean isDigit0Letter = Character.isLetter(digit0); boolean isSymbolDollarLetter = Character.isLetter(symbolDollar); boolean isSpaceLetter = Character.isLetter(space); boolean isNewLineLetter = Character.isLetter(newLine); boolean isCommaLetter = Character.isLetter(comma); boolean isUpperCaseALetter = Character.isLetter(upperCaseA); System.out.println("Is '" + digit0 + "' a letter? " + isDigit0Letter); System.out.println("Is '" + symbolDollar + "' a letter? " + isSymbolDollarLetter); System.out.println("Is space a letter? " + isSpaceLetter); // Print "space" instead of the character itself for clarity System.out.println("Is newline a letter? " + isNewLineLetter); // Print "newline" System.out.println("Is '" + comma + "' a letter? " + isCommaLetter); System.out.println("Is '" + upperCaseA + "' a letter? " + isUpperCaseALetter); }We've added examples for a digit, a symbol, a space, a newline character, and a comma. We also kept an uppercase letter for comparison. Notice how we print "space" and "newline" for clarity, as printing the characters themselves might not be visible or clear in the output.
Save the modified
LetterCheck.javafile (Ctrl + SorCmd + S).Open the Terminal and ensure you are in the
~/projectdirectory.Compile the updated Java file:
javac LetterCheck.javaIf the compilation is successful, you will see no output.
Run the compiled program:
java LetterCheckYou should see output similar to this:
Is '0' a letter? false Is '$' a letter? false Is space a letter? false Is newline a letter? false Is ',' a letter? false Is 'A' a letter? true
This output demonstrates that Character.isLetter() correctly identifies digits, symbols, spaces, and newline characters as non-letters. This confirms that the method is specifically designed to check for characters that are part of an alphabet.
You have now successfully used Character.isLetter() to check various types of characters, including uppercase letters, lowercase letters, digits, symbols, and whitespace. This method is useful when you need to process text and identify or filter out letter characters.
Summary
In this lab, we learned how to use the Character.isLetter() method in Java to determine if a given character is a letter. We created a Java file, LetterCheck.java, and wrote code to test this method with various characters, including uppercase letters, lowercase letters, digits, and symbols. By using Character.isLetter(), we were able to obtain a boolean result indicating whether each character was a letter and print these results to the console. This hands-on exercise demonstrated the basic usage of the Character.isLetter() method for character classification in Java.



