Handle Non-Letter Characters
In the previous steps, you've seen how Character.isLowerCase()
works with both lowercase and uppercase letters, as well as some non-letter characters like digits, symbols, and spaces. It's important to remember that Character.isLowerCase()
is specifically designed to check for lowercase letters. It will return false
for any character that is not considered a letter in the Unicode standard, regardless of whether it appears in a "lowercase" form (like a lowercase digit, which doesn't exist).
Let's create a slightly different program that focuses on checking various non-letter characters to reinforce this understanding.
-
Open the File Explorer on the left side of the WebIDE.
-
Navigate to the ~/project
directory.
-
Right-click in the empty space within the ~/project
directory and select "New File".
-
Name the new file NonLetterCheck.java
.
-
Open the NonLetterCheck.java
file in the editor.
-
Copy and paste the following code into the editor:
public class NonLetterCheck {
public static void main(String[] args) {
char digit = '7';
char symbol = '#';
char space = ' ';
char newline = '\n'; // Newline character
char tab = '\t'; // Tab character
System.out.println("Is '" + digit + "' lowercase? " + Character.isLowerCase(digit));
System.out.println("Is '" + symbol + "' lowercase? " + Character.isLowerCase(symbol));
System.out.println("Is ' ' lowercase? " + Character.isLowerCase(space));
System.out.println("Is newline lowercase? " + Character.isLowerCase(newline));
System.out.println("Is tab lowercase? " + Character.isLowerCase(tab));
}
}
In this program, we are explicitly testing characters that are not letters: a digit, a symbol, a space, a newline character (\n
), and a tab character (\t
).
-
Save the file (Ctrl + S
or Cmd + S
).
Now, compile and run this new program.
-
Open the Terminal at the bottom of the WebIDE. Ensure you are in the ~/project
directory.
-
Compile the Java file:
javac NonLetterCheck.java
-
Run the compiled Java program:
java NonLetterCheck
You should see the following output:
Is '7' lowercase? false
Is '#' lowercase? false
Is ' ' lowercase? false
Is newline lowercase? false
Is tab lowercase? false
As expected, Character.isLowerCase()
returns false
for all these non-letter characters. This confirms that the method is specifically for checking if a character is a lowercase letter.
Understanding the behavior of methods like Character.isLowerCase()
with different types of input is fundamental to writing correct and predictable code. In the next steps, you might explore other methods in the Character
class, such as isUpperCase()
, isDigit()
, or isLetter()
.