java.lang.Character getNumericValue(char ch)

Description

The Character.getNumericValue(char ch) java method returns the int value that the specified Unicode character represents. For example, the character ‘u216C’ (the roman numeral fifty) will return an int with a value of 50.

The letters A-Z in their uppercase (‘u0041’ through ‘u005A’), lowercase (‘u0061’ through ‘u007A’), and full width variant (‘uFF21’ through ‘uFF3A’ and ‘uFF41’ through ‘uFF5A’) forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.

The getNumericValue(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.getNumericValue(char ch)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java getNumericValue() method non statically.

Notes:

  • This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the getNumericValue(int) method.

Method Syntax

public static int getNumericValue(char ch)

Method Argument

Data Type Parameter Description
char ch the character to be converted.

Method Returns

The getNumericValue(char ch) method of Character class returns an int which denotes the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value that is not a nonnegative integer; -1 if the character has no numeric value.

Compatibility

Requires Java 1.1 and up

Java Character getNumericValue(char ch) Example

Below is a simple java example on the usage of getNumericValue(char ch) method of Character class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * getNumericValue(char ch) method of Character class.
 */

public class CharacterGetNumericValueCharExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an input:");

		// use scanner to get the user input
		Scanner s = new Scanner(System.in);

		// gets the user input
		char[] value = s.nextLine().toCharArray();

		// close the scanner object
		s.close();

		// get numeric value of user input
		for (char ch : value) {
			int result = Character.getNumericValue(ch);
			// print the result
			System.out.println("character " + ch 
					+ " numeric value is " + result);
		}

	}

}

Sample Output

Below is the sample output when you run the above example.

java Character getNumericValue(char ch) example output