New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Character digit(char ch, int radix) Method

Java digit() method is a part of Character class. This method returns a numeric value of the character. It must be noted that if :

  • The radix is not in the range MIN_RADIX<=radix<=MAX_RADIX , or
  • The value of ch 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 point is true:

  • The method isDigit() 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(char ch, int radix) 

Parameter:

The parameter passed is the char ch for the conversion 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) 
    {      
        char ch1 = 'A';  
        char ch2 = '8';          
        int a = Character.digit(ch1, 8);  
        int b = Character.digit(ch2, 16);         
        System.out.println("The numeric value of " + ch1 + " in radix 8 is :"+a);  
        System.out.println("The numeric value of " + ch2  + " in radix 16 is :"+b);             
     }  
}  


The numeric value of A in radix 8 is :-1
The numeric value of 8 in radix 16 is :8

Example 2:

Here is an 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 character:");  
			Scanner sc = new Scanner(System.in);  
			char[] ch = sc.nextLine().toCharArray();  
			System.out.print("Enter radix:");  
			int radix = sc.nextInt();        
			for (char charval : ch) 
			{  
				int digit = Character.digit(charval, radix);  
				System.out.println("The numeric value is: " + digit);  
			}
		} 
		catch(Exception e)
		{
			System.out.println("Invalid input");  
		}
	}  
}  


Enter character: studytonight
Enter radix: 4
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
The numeric value is: -1
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.