Java Character Codepointat Charsequence Int Method

JavaJavaBeginner
Practice Now

Introduction

The codePointAt(CharSequence seq, int index) method is used to get the Unicode code point of the character at the specified index in a CharSequence.


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/format("`Format`") 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/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/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-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/format -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/classes_objects -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/class_methods -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/modifiers -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/oop -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/packages_api -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/user_input -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/wrapper_classes -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/identifier -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/arrays -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/data_types -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/operators -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/output -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/strings -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/variables -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/string_methods -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} java/system_methods -.-> lab-117479{{"`Java Character Codepointat Charsequence Int Method`"}} end

Create a Java program file

Create a Java program file called CodePointAtDemo.java in the ~/project directory using the following command:

touch ~/project/CodePointAtDemo.java

Write the Java code

Write the following Java code in the CodePointAtDemo.java file:

import java.lang.Character;
import java.util.Scanner;

public class CodePointAtDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scan.nextLine();

        System.out.print("Enter an index: ");
        int index = scan.nextInt();

        char ch = input.charAt(index);
        int codePoint = Character.codePointAt(input, index);

        System.out.printf("The Unicode code point of '%c' at index %d is %d", ch, index, codePoint);
    }
}

In the above code, we import the Character class and Scanner class. We then create a main method which takes user input, gets the Unicode code point of the character at index index in the input string, and then displays the character and its Unicode code point.

Compile and run the program

Compile the CodePointAtDemo.java program by running the following command:

javac ~/project/CodePointAtDemo.java

After the compilation is successful, run the program using the following command:

java CodePointAtDemo

You should see the following prompt:

Enter a string:

Enter a string of your choice and press enter. You should see the following prompt:

Enter an index:

Enter an index of your choice and press enter. The program will display the character and its Unicode code point at the specified index.

For example:

Enter a string: Hello world
Enter an index: 1
The Unicode code point of 'e' at index 1 is 101

Summary

Congratulations! You have successfully completed the Java Character Codepointat Charsequence Int Method lab. You have learned how to use the codePointAt(CharSequence seq, int index) method of the Character class in the Java programming language to get the Unicode code point of a character at a specified index in a CharSequence.

Other Java Tutorials you may like