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.

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