Determining Titlecase Characters in Java

JavaJavaBeginner
Practice Now

Introduction

isTitleCase(int codePoint) is a method in the Java Character class. It checks whether the specified Unicode codepoint character is a Titlecase character or not. This method supports supplementary characters. A character is a title case character if its general category type, provided by Character.getType(ch), is TITLECASE_LETTER.

Create a Java file

Create a new Java file using the following command:

touch Test.java

Add code to check if a character is a title case character

Add the following code to the Test.java file. This code checks whether five different Unicode codepoint characters are Titlecase characters or not.

public class Test {
  public static void main(String[] args) {
    int cp1 = 0x01c8;
    int cp2 = 60;
    int cp3 = 119;
    int cp4 = 0x01c1;
    int cp5 = 1232;

    boolean b1 = Character.isTitleCase(cp1);
    boolean b2 = Character.isTitleCase(cp2);
    boolean b3 = Character.isTitleCase(cp3);
    boolean b4 = Character.isTitleCase(cp4);
    boolean b5 = Character.isTitleCase(cp5);

    System.out.println((char) cp1 + " is a title case??:  " + b1);
    System.out.println((char) cp2 + " is a title case??:  " + b2);
    System.out.println((char) cp3 + " is a title case??:  " + b3);
    System.out.println((char) cp4 + " is a title case??:  " + b4);
    System.out.println((char) cp5 + " is a title case??:  " + b5);
  }
}

Compile and run the code

Compile the code using the following command:

javac Test.java

Run the code using the following command:

java Test

Provide user input

Add the following code to the Test.java file. This code takes user input and checks whether the provided character is a Titlecase character or not.

import java.util.Scanner;

public class Test {
  public static void main(String[] args) {
    try {
      System.out.print("Enter the Unicode character: ");
      Scanner sc = new Scanner(System.in);
      int cp = sc.nextInt();
      boolean b = Character.isTitleCase(cp);
      System.out.println((char) cp + " is a title case?: " + b);
    } catch (Exception e) {
      System.out.println("Invalid Input!!");
    }
  }
}

Compile and run the code

Compile the code using the following command:

javac Test.java

Run the code using the following command:

java Test

Provide a Unicode character as input and the code will tell you whether it's a Titlecase character or not.

Summary

In this lab, you learned how to use the isTitleCase(int codePoint) method in the Java Character class. You checked whether a specified Unicode codepoint character is a Titlecase character or not. By completing this lab, you should now be able to check whether a particular character is a Titlecase character.

Other Java Tutorials you may like