Introduction
In this lab, you will learn about the Java isTitleCase(char ch) method. This method is used to check whether the specified character is a Titlecase character or not. We will cover the following topics in this lab:
Create a new Java file
Create a new Java file called CharacterTitleCase.java in the ~/project directory using the following command:
touch ~/project/CharacterTitleCase.java
Declare the main method
In this step, we will declare the main method. The main method is the entry point of our program.
Add the following code to your CharacterTitleCase.java file:
public class CharacterTitleCase {
public static void main(String[] args) {
}
}
Use the isTitleCase(char ch) method
In this step, we will use the isTitleCase(char ch) method to check whether the specified character is a Titlecase character or not.
Add the following code inside the main method:
char ch = 'A'; // character to check
boolean isTitleCase = Character.isTitleCase(ch); // check Titlecase
if (isTitleCase) {
System.out.println(ch + " is a Titlecase character.");
} else {
System.out.println(ch + " is not a Titlecase character.");
}
Test the program
Now, let's test the program. Compile and run the CharacterTitleCase.java file using the following command:
javac CharacterTitleCase.java && java CharacterTitleCase
You will see the output as follows:
A is a Titlecase character.
Multiple character's check
In this step, let's use multiple characters to check whether they are Titlecase characters or not.
Add the following code inside the main method:
String chars = "ABcdEFgh12"; // characters to check
for (int i = 0; i < chars.length(); i++) {
char ch = chars.charAt(i); // get character at i-th index
boolean isTitleCase = Character.isTitleCase(ch); // check Titlecase
if (isTitleCase) {
System.out.println(ch + " is a Titlecase character.");
} else {
System.out.println(ch + " is not a Titlecase character.");
}
}
Test the program
Now, let's test the program again. Compile and run the CharacterTitleCase.java file using the following command:
javac CharacterTitleCase.java && java CharacterTitleCase
You will see the output as follows:
A is a Titlecase character.
B is a Titlecase character.
c is not a Titlecase character.
d is not a Titlecase character.
E is a Titlecase character.
F is a Titlecase character.
g is not a Titlecase character.
h is not a Titlecase character.
1 is not a Titlecase character.
2 is not a Titlecase character.
User input
In this step, we will allow the user to input a character and check whether it is a Titlecase character or not.
Add the following code inside the main method:
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = input.nextLine().charAt(0); // read user input
boolean isTitleCase = Character.isTitleCase(ch); // check Titlecase
if (isTitleCase) {
System.out.println(ch + " is a Titlecase character.");
} else {
System.out.println(ch + " is not a Titlecase character.");
}
input.close();
Test the program
Now, let's test the program again. Compile and run the CharacterTitleCase.java file using the following command:
javac CharacterTitleCase.java && java CharacterTitleCase
You will see the program prompting you to enter a character.
Enter a character:
Enter a character of your choice and press enter. The program will check whether the entered character is a Titlecase character or not.
Enter a character: C
C is a Titlecase character.
Edge case
In this step, let's add an edge case where we will test whether the method can handle Unicode characters.
Add the following code inside the main method:
char ch = '\u01F2'; // Unicode character to check
boolean isTitleCase = Character.isTitleCase(ch); // check Titlecase
if (isTitleCase) {
System.out.println(ch + " is a Titlecase character.");
} else {
System.out.println(ch + " is not a Titlecase character.");
}
Test the program
Compile and run the CharacterTitleCase.java file using the following command:
javac CharacterTitleCase.java && java CharacterTitleCase
You will see the output as follows:
Dz is a Titlecase character.
Summary
In this lab, you learned about the Java isTitleCase(char ch) method. You also learned how to use isTitleCase(char ch) method and its implementation in the program.
Here are some key takeaways from this lab:
isTitleCase(char ch)method is used to check whether the specified character is a Titlecase character or not.- A character is a title case character if its general category type, provided by
Character.getType(ch), isTITLECASE_LETTER. - This method does not support supplementary characters.
- You can use a for loop to check multiple characters in one go.
- User input can easily be incorporated into the program.



