Signup/Sign In

Java Character forDigit() Method

Java forDigit() method is a part of Character class. This method returns the specific character representation for the specified digit in accordance with the specified radix. It must be noted that if the value of the int digit is not a valid digit in the specified radix or if the value of the radix is not a valid radix then a null character is returned.

Also, the value of radix is valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX.

The digit argument is valid if 0 <= digit < radix.

If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned.


public static char forDigit(int digit, int radix)  


The parameters passed are the digit to be converted into character and radix which would provide the base for conversion.


Returns the char representation of the specified digit in the specified index.

Example 1:

Here, the char representations of the specified digits are returned in accordance with the radix specified.

public class StudyTonight 
    public static void main(String[] args)
        int n1 = 8;  
        int n2 = 23;  
        int n3 = 30;       
        char ch1 = Character.forDigit(n1, 9);  
        char ch2 = Character.forDigit(n2, 25);  
        char ch3 = Character.forDigit(n3, 36);  
        System.out.println("The char representation of " + n1 + " in radix 9 is " + ch1);  
        System.out.println("The char representation of " + n2 + " in radix 25 is " + ch2);  
        System.out.println("The char representation of " + n3 + " in radix 36 is " + ch3); 

The char representation of 8 in radix 9 is 8
The char representation of 23 in radix 25 is n
The char representation of 30 in radix 36 is u

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)
			Scanner sc = new Scanner(;
			System.out.print("Enter digit:"); 
			int d = sc.nextInt();
			System.out.print("Enter radix:");  
			int r = sc.nextInt();  
			char result = Character.forDigit(d, r);  
			System.out.println("The character at radix \'" + r + "\' for the digit " + d + "  is : " + result);   
		catch(Exception e)
			System.out.println("Invalid Input");

Enter digit: 12
Enter radix: 16
The character at radix '16' for the digit 12 is : c
Enter digit: 6
Enter radix: 10
The character at radix '10' for the digit 6 is : 6

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.