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.


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/recursion("`Recursion`") java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") 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/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/recursion -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/scope -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/classes_objects -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/class_methods -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/modifiers -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/oop -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/wrapper_classes -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/identifier -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/arrays -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/comments -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/data_types -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/operators -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/output -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/strings -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/variables -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/string_methods -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} java/system_methods -.-> lab-117483{{"`Counting Unicode Code Points in Char Array`"}} end

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