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.


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/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") 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/scope -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/classes_objects -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/class_methods -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/exceptions -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/modifiers -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/oop -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/packages_api -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/user_input -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/wrapper_classes -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/identifier -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/arrays -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/comments -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/data_types -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/operators -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/output -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/strings -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/variables -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/string_methods -.-> lab-117485{{"`Counting Unicode Code Points`"}} java/system_methods -.-> lab-117485{{"`Counting Unicode Code Points`"}} end

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