Counting Unicode Code Points

JavaJavaBeginner
Practice Now

Introduction

The codePointCount() method belongs to the Character class in Java, which is used to count the number of Unicode code points in the given text range of a character sequence. In this lab, we will learn how to use the Character.codePointCount() method by performing some examples.

Create a new Java file

First, we need to create a Java file. Open the terminal and move to the desired directory where you want to create the file and then type the following command to create the new file.

touch CodePointCount.java

Initialize the code skeleton

To get started with the code, import the java.lang.Character package, and define a class CodePointCount with a main() method.

import java.lang.Character;

public class CodePointCount {
    public static void main(String[] args) {
        // code here
    }
}

Count the Unicode code points in a given text range

Now, we will write code to count and display the number of Unicode code points present in the given text range.

CharSequence seq = "Learning Java is fun"; // text sequence
int beginIndex = 0; // start index of text range
int endIndex = seq.length(); // end index of text range

int count = Character.codePointCount(seq, beginIndex, endIndex); // count code points
System.out.println("Count of code points: " + count);

Run and compile the code

After writing the complete code, it's time to compile and run the program. Open the terminal and move to the directory containing the Java file.

To compile the Java file:

javac CodePointCount.java

To run the program:

java CodePointCount

Take user input and display code point count

In this step, we will take input from the user and display the number of Unicode code points present in the given text range.

try {
    Scanner sc = new Scanner(System.in); // Scanner class object to take user input
    System.out.println("Enter a character sequence: ");
    CharSequence seq = sc.nextLine(); // take user input sequence
    System.out.println("Enter the starting index of the text range: ");
    int beginIndex = sc.nextInt(); // take user input begin index
    System.out.println("Enter the end index of the text range: ");
    int endIndex = sc.nextInt(); // take user input end index

    int count = Character.codePointCount(seq, beginIndex, endIndex); // count code points
    System.out.println("Count of code points: " + count);
} catch (Exception e) {
    System.out.println("Invalid input");
}

Run and compile the code

Compile the code using the following command in the terminal

javac CodePointCount.java

and to run the program, type the following command.

java CodePointCount

Test the program with invalid input

Let's now test the program with invalid input values.

// enter invalid input values
CharSequence seq = "Java is a popular programming language"; // text sequence
int beginIndex = 10; // start index of text range
int endIndex = 5; // end index of text range

int count = Character.codePointCount(seq, beginIndex, endIndex); // count code points
System.out.println("Count of code points: " + count);

Run and compile the code

Compile the code using the following command in the terminal

javac CodePointCount.java

and to run the program, type the following command.

java CodePointCount

Summary

In this lab, we learned the following concept about Java Character CodePointCount() Method:

  • What is the Character.codePointCount() method in Java?
  • How to use the Character.codePointCount() method in Java?
  • How to count the Unicode code points in a given text range?
  • How to apply exception handling to handle invalid input values?

Other Java Tutorials you may like