Counting Unicode Code Points in Char Array

JavaJavaBeginner
Practice Now

Introduction

The Java codePointCount() method returns the total Unicode code point count of the sub-array of the specified char array. It is a part of Character class in Java. The offset parameter denotes the starting index of the char array, while the count parameter is used to determine the number of characters to consider.

Create a Java file

Create a file named CharacterCodepointCount.java in the ~/project directory by using the following command:

touch ~/project/CharacterCodepointCount.java

Open the file in a text editor.

Declare the codePointCount() method

Inside the CharacterCodepointCount class, declare the codePointCount() method that takes in three parameters: char[] a, int offset, and int count. It should be declared as a static method since we will be calling it directly from the main method.

public class CharacterCodepointCount {
    public static int codePointCount(char[] a, int offset, int count) {
        // code for the method
    }
}

In the above code, we have declared a static method codePointCount() that takes an array of characters (char[] a), an integer value of the starting point (int offset), and an integer value of the count (int count) as its parameters.

Implement the codePointCount() method

Inside the codePointCount() method, write the code to return the total Unicode code point count of the sub-array of the specified char array.

public class CharacterCodepointCount {
    public static int codePointCount(char[] a, int offset, int count) {
        return Character.codePointCount(a, offset, count);
    }
}

In the above code, we have returned the total Unicode code point count of the sub-array of the specified char array using the codePointCount() method of the Character class.

Use the codePointCount() method in the main method

In the main() method, we will use the codePointCount() method to find the total Unicode code point count of the sub-array of the specified char array.

public class CharacterCodepointCount {
    public static void main(String[] args) {
        char[] ch1 = new char[] { 'j', 'a', 'v', 'a', '1', '2', '3' };
        int offset1 = 0, count1 = 3;
        int r1 = codePointCount(ch1, offset1, count1);
        System.out.println("The number of Unicode code points in the subarray is: " + r1);

        String s1 = "Hello World";
        int offset2 = 2, count2 = 4;
        int r2 = s1.codePointCount(offset2, count2);
        System.out.println("The number of Unicode code points in the subarray is: " + r2);
    }

    public static int codePointCount(char[] a, int offset, int count) {
        return Character.codePointCount(a, offset, count);
    }
}

In the above code, we have created two arrays of characters and used them as parameters to the codePointCount() method. Then we printed out the total Unicode code point count of the sub-array of the specified char array using the println() method.

Compile and run the code

Save the file and open the terminal. Use the following command to compile the code:

javac ~/project/CharacterCodepointCount.java

If there are no errors, run the program with the following command:

java CharacterCodepointCount

The output will be:

The number of Unicode code points in the subarray is: 3
The number of Unicode code points in the subarray is: 4

Summary

In this lab, you have learned how to use the codePointCount() method in Java to find the total Unicode code point count of the sub-array of the specified char array. You have also learned how to create a Java file, implement a method, use arrays and strings as method parameters, and compile and run the Java program from the terminal.

Other Java Tutorials you may like