Signup/Sign In

Java Character getNumericValue(int codePoint) Method

Java getNumericValue(int codepoint) method is a part of Character class. This method returns the integer value of the character at the Unicode codepoint specified. It returns -1 if the character does not have any int value and -2 if the character cannot be represented as a non-negative integer(for example a fraction or a decimal value).

Syntax:

public static int getNumericValue(int codepoint)  

Parameters:

The parameter passed is the Unicode codepoint character whose integer value is to be returned.

Returns:

Returns the value of the specified character as a non-negative integer.

Example 1:

Here, the numerical int non-negative value of the Unicode codepoint characters is passed.

public class StudyTonight 
{  
	public static void main(String[] args)
	{         
		int cp1 = 66;  
		int cp2 = 53;  
		int cp3 = 03;  
		int cp4 = 90;  
		int cp5 = 84;   
		int r1 = Character.getNumericValue(cp1); //return int value for the specified code point
		int r2 = Character.getNumericValue(cp2);  
		int r3 = Character.getNumericValue(cp3);  
		int r4 = Character.getNumericValue(cp4);
		int r5 = Character.getNumericValue(cp5);      
		System.out.println("The character " + (char)cp1 + " has int value : " + r1);  
		System.out.println("The character " + (char)cp2 + " has int value : " + r2);  
		System.out.println("The character " + (char)cp3 + " has int value : " + r3);  
		System.out.println("The character " + (char)cp4 + " has int value : " + r4);
		System.out.println("The character " + (char)cp5 + " has int value : " + r5);      
	}
} 


The character B has int value : 11
The character 5 has int value : 5
The character  has int value : -1
The character Z has int value : 35
The character T has int value : 29

Example 2:

Here is a general example where the user can enter the input of his choice and get the desired output.

import java.util.Scanner;
public class StudyTonight 
{  
	public static void main(String[] args)
	{         
		try
		{
			System.out.println("Enter the codepoint"); 
			Scanner sc = new Scanner(System.in);
			int cp = sc.nextInt();

			int r = Character.getNumericValue(cp);  

			System.out.println("The character " + cp + " has value : " + r);
		}
		catch(Exception e)
		{
			System.out.println("Invalid input");
		}
	}
} 


Enter the codepoint
77
The character 77 has value : 22
*************************************
Enter the codepoint
12
The character 12 has value : -1
*************************************
Enter the codepoint
o
Invalid input

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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.