Exploring Java Character Title Case Method

JavaJavaBeginner
Practice Now

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:


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/classes_objects -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/class_methods -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/modifiers -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/oop -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/user_input -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/wrapper_classes -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/identifier -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/arrays -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/comments -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/data_types -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/for_loop -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/if_else -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/operators -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/output -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/strings -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/variables -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/string_methods -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} java/system_methods -.-> lab-117555{{"`Exploring Java Character Title Case Method`"}} end

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) , is TITLECASE_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.

Other Java Tutorials you may like