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.

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