Convert Unicode Code Point to Title Case

JavaJavaBeginner
Practice Now

Introduction

The toTitleCase(int codePoint) method of the Character class converts the specified Unicode code point character argument to titlecase using case mapping information from the UnicodeData file. This lab provides a step-by-step guide to use this method with examples.


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/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") 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/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/classes_objects -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/class_methods -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/exceptions -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/modifiers -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/oop -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/packages_api -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/user_input -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/wrapper_classes -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/identifier -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/arrays -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/comments -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/data_types -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/operators -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/output -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/strings -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/variables -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} java/system_methods -.-> lab-117592{{"`Convert Unicode Code Point to Title Case`"}} end

Create a Java class

Create a new Java class in the ~/project directory using the following command:

cd ~/project
touch ToTitleCase.java

Add the code

Add the following code to the ToTitleCase.java file to convert the specified Unicode code point character argument to titlecase:

import java.util.Scanner;

public class ToTitleCase {

    public static void main(String[] args) {

        // Example 1
        int cp1 = 78;
        int cp2 = 102;
        int cp3 = 66;
        int cp4 = 48;
        int cp5 = 1232;

        char ch1 = Character.toTitleCase(cp1);
        char ch2 = Character.toTitleCase(cp2);
        char ch3 = Character.toTitleCase(cp3);
        char ch4 = Character.toTitleCase(cp4);
        char ch5 = Character.toTitleCase(cp5);

        System.out.println("Example 1:");
        System.out.println("The titlecase character of 78 is :"+ch1);
        System.out.println("The titlecase character of 102 is :"+ch2);
        System.out.println("The titlecase character of 66 is :"+ch3);
        System.out.println("The titlecase character of 48 is :"+ch4);
        System.out.println("The titlecase character of 1232 is :"+ch5);

        // Example 2
        try {
            System.out.println("\nExample 2:");
            System.out.print("Enter the Unicode codepoint: ");
            Scanner sc = new Scanner(System.in);
            int cp = sc.nextInt();
            char cc = Character.toTitleCase(cp);
            System.out.println("The titlecase character is : "+cc);
        }
        catch(Exception e)
        {
            System.out.println("Invalid Input!!");
        }
    }
}

The first example converts various code points to their equivalent titlecase characters, while the second example is a user-defined code block which takes input from the user and returns the titlecase character of the input Unicode code point value.

Compile and run the code

Compile the code using the following command:

javac ToTitleCase.java

This will generate the ToTitleCase.class file.

Now, run the code using the following command:

java ToTitleCase

This will execute the code and show the output in the terminal.

Summary

In this lab, you learned how to use the toTitleCase(int codePoint) method of the Character class in Java to convert a specified Unicode code point character argument to titlecase. You also learned how to run an example to test the function of the method.

Other Java Tutorials you may like