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.

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