Java Character isValidCodePoint Method

JavaJavaBeginner
Practice Now

Introduction

The isValidCodePoint() method is a part of the Java Character class. This method is used to check whether the specified Unicode code point is a valid Unicode code point value or not. This lab will guide you on how to use the isValidCodePoint() method in your Java program.


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/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") 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/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/classes_objects -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/class_methods -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/modifiers -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/oop -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/user_input -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/wrapper_classes -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/identifier -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/arrays -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/data_types -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/operators -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/output -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/strings -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/type_casting -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/variables -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} java/system_methods -.-> lab-117570{{"`Java Character isValidCodePoint Method`"}} end

Creating a Main Method

The first step is to create a main method in the CharIsValidCodePoint class. The main method is the entry point of the Java application. Add the following code block to the CharIsValidCodePoint.java file:

public class CharIsValidCodePoint {
    public static void main(String[] args) {

    }
}

Creating Unicode Code Points

In this step, we will create some Unicode code points to test the isValidCodePoint() method. Add the following code block inside the main method:

int cp1 = 73;
int cp2 = 60;
int cp3 = 119;
int cp4 = 0x0123;
int cp5 = 0x123fff;

These variables are used to store the Unicode code points which we will check using the isValidCodePoint() method.

Check if Unicode Point is Valid

In this step, we will use the isValidCodePoint() method to check if the Unicode code points we created in Step 2 are valid or not. Add the following code block inside the main method:

boolean b1 = Character.isValidCodePoint(cp1);
boolean b2 = Character.isValidCodePoint(cp2);
boolean b3 = Character.isValidCodePoint(cp3);
boolean b4 = Character.isValidCodePoint(cp4);
boolean b5 = Character.isValidCodePoint(cp5);

These variables are used to store the boolean value returned by the isValidCodePoint() method for each Unicode code point.

Print the Result of Each Unicode Point

In this step, we will print the result of each Unicode code point. Add the following code block inside the main method:

System.out.println((char)cp1 + " is a valid Unicode code point? " + b1);
System.out.println((char)cp2 + " is a valid Unicode code point? " + b2);
System.out.println((char)cp3 + " is a valid Unicode code point? " + b3);
System.out.println((char)cp4 + " is a valid Unicode code point? " + b4);
System.out.println((char)cp5 + " is a valid Unicode code point? " + b5);

This code will print the Unicode code point and the boolean value returned by the isValidCodePoint() method.

Compile and Run the Program

In this step, we will compile and run the program. Use the following command to compile the CharIsValidCodePoint.java file:

javac CharIsValidCodePoint.java

Once the compilation is successful, use the following command to run the CharIsValidCodePoint program:

java CharIsValidCodePoint

The output of the program will look like this:

I is a valid Unicode code point? true
< is a valid Unicode code point? true
w is a valid Unicode code point? true
? is a valid Unicode code point? true
? is a valid Unicode code point? false

User Input

In this step, we will take user input for a Unicode code point and check if it is valid or not. Add the following code block inside the main method:

Scanner input = new Scanner(System.in);
System.out.print("Enter a Unicode code point: ");
int codePoint = input.nextInt();
boolean valid = Character.isValidCodePoint(codePoint);
System.out.println("The Unicode code point " + codePoint + " is valid? " + valid);

This code will take user input for a Unicode code point, check if it is valid or not using the isValidCodePoint() method, and print the result.

Compile and Run the Program

In this step, we will compile and run the program again. Use the following command to compile the CharIsValidCodePoint.java file:

javac CharIsValidCodePoint.java

Once the compilation is successful, use the following command to run the updated CharIsValidCodePoint program:

java CharIsValidCodePoint

The output of the program will look like this:

Enter a Unicode code point: 11
The Unicode code point 11 is valid? true

Testing Additional Code Points

In this step, we will test isValidCodePoint() method using additional Unicode code points. Add the following code block inside the main method:

int codePoint1 = 400;
int codePoint2 = 500;
int codePoint3 = 600;

boolean valid1 = Character.isValidCodePoint(codePoint1);
boolean valid2 = Character.isValidCodePoint(codePoint2);
boolean valid3 = Character.isValidCodePoint(codePoint3);

System.out.println("The Unicode code point " + codePoint1 + " is valid? " + valid1);
System.out.println("The Unicode code point " + codePoint2 + " is valid? " + valid2);
System.out.println("The Unicode code point " + codePoint3 + " is valid? " + valid3);

This code will create three additional Unicode code points and test if they are valid using the isValidCodePoint() method.

Compile and Run the Program

In this step, we will compile and run the program again. Use the following command to compile the CharIsValidCodePoint.java file:

javac CharIsValidCodePoint.java

Once the compilation is successful, use the following command to run the updated CharIsValidCodePoint program:

java CharIsValidCodePoint

The output of the program will look like this:

Enter a Unicode code point: 11
The Unicode code point 11 is valid? true
The Unicode code point 400 is valid? true
The Unicode code point 500 is valid? true
The Unicode code point 600 is valid? true

Summary

To summarize, the Java isValidCodePoint() method is an important tool to check if a Unicode code point is valid or not. With its help, you can ensure that your program handles Unicode data correctly.

Other Java Tutorials you may like