Signup/Sign In

Java Character digit(int codePoint, int radix) Method

Java digit() method is a part of Character class. This method returns the numeric value of the character to the specified codepoint value.

It must be noted that if :

  • The radix is not in the range MIN_RADIX<=radix<=MAX_RADIX , or
  • The character is not a valid digit in a particular radix, then 1 is returned

Also, a character is a valid digit if at least one of the following is true:

  • The method isdigit(int codepoint) is true for the character and the Unicode decimal is less than the specified radix. In this case, the decimal digit value is returned.
  • If the character is one of the uppercase letters 'A' to 'Z' and its code is less than the radix, for + 'A', -10 is returned and for 'A', +10 is returned.
  • If the character is one of the lowercase letters 'a' to 'z' and its code is less than the radix, for + 'a', -10 is returned and for -'a', +10 is returned.

Syntax:

public static int digit(int codePoint, int radix) 

Parameter:

The parameter passed is the int codePoint which is the character to be converted and the int radix to provide a base for the conversion.

Returns:

Returns the numerical value represented by the character at the specified index.

Example 1:

Here, the numerical value of the specified character is returned with respect to the specified index.

import java.util.Scanner; 
import java.lang.Character;
public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
        int codePoint = 99;  
        int radix = 16;               
        char ch = (char) codePoint;           
        int digit = Character.digit(codePoint,radix);  
        System.out.println("The numeric value of "+ch+" is : " +digit);  
    }  
}  


The numeric value of c is : 12

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; 
import java.lang.Character;
public class StudyTonight 
{  
	public static void main(String[] args) 
	{      
		try
		{
			System.out.print("Enter codepoint value:");  
			Scanner sc = new Scanner(System.in);  
			int cp = sc.nextInt();
			System.out.print("Enter radix:");  
			int radix = sc.nextInt();        

			int digit = Character.digit(cp, radix);  
			System.out.println("The numeric value is: " + digit);            
		} 
		catch(Exception e)
		{
			System.out.println("Invalid input");  
		}
	}  
}  


Enter codepoint value: 889
Enter radix: 5
The numeric value is: -1
********************************
Enter codepoint value: 5
Enter radix: 4
The numeric value is: -1

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.