LAST UPDATED: NOVEMBER 24, 2020
Java Character isValidCodePoint(int codePoint) Method
Java isValidCodePoint()
method is a part of Character
class. This method is used to check whether the specified Unicode codepoint is a valid Unicode codepoint value or not.
Syntax:
public static boolean isValidCodePoint(int codePoint)
Parameters:
The parameter passed is the Unicode codepoint to be checked whether it is valid or not.
Returns:
Returns the boolean value true
if the specified code point value is between MIN_CODE_POINT
and MAX_CODE_POINT
inclusive else return false
Example 1:
Here, the characters are checked whether they are valid codepoint values or not.
public class StudyTonight
{
public static void main(String[] args)
{
int cp1 = 73;
int cp2 = 60;
int cp3 = 119;
int cp4 = 0x0123;
int cp5 = 0x123fff;
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);
System.out.println((char)cp1 +" is valid codepoint value??:: "+b1);
System.out.println((char)cp2 +" is valid codepoint value??:: "+b2);
System.out.println((char)cp3 +" is valid codepoint value??:: "+b3);
System.out.println((char)cp4 +" is valid codepoint value??:: "+b4);
System.out.println((char)cp5 +" is valid codepoint value??:: "+b5);
}
}
I is valid codepoint value??:: true
< is valid codepoint value??:: true
w is valid codepoint value??:: true
? is valid codepoint value??:: true
? is valid codepoint value??:: false
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the Unicode character: ");
Scanner sc = new Scanner(System.in);
int cp = sc.nextInt();
boolean b = Character.isValidCodePoint(cp);
System.out.println((char)cp + " is a valid codepoint??: "+b);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
Enter the Unicode character: 11
is a valid codepoint??: true
*****************************************
Enter the Unicode character: 787
? is a valid codepoint??: true
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.