Introduction
In this lab, you will learn about the Java forDigit()
method which is a part of the Character
class. This method returns the specified character representation for the specified digit in accordance with the specified radix.
In this lab, you will learn about the Java forDigit()
method which is a part of the Character
class. This method returns the specified character representation for the specified digit in accordance with the specified radix.
Create a Java file named CharForDigit.java
in the ~/project
directory using the following command:
touch ~/project/CharForDigit.java
Add the following code to the CharForDigit.java
file:
public class CharForDigit {
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);
}
}
This code creates a CharForDigit
class with a main
method. The main
method demonstrates how to use the forDigit()
method to get the character representation of a digit in different radix.
Compile the CharForDigit.java
file using the following command:
javac CharForDigit.java
Then, run the code using the following command:
java CharForDigit
You should see the following output:
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
This output confirms that the forDigit()
method works as expected.
Now, let's modify the code to accept user input for the digit and radix. Add the following code after the initial code:
Scanner sc = new Scanner(System.in);
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);
This code prompts the user for the digit and radix, reads the input from the console, and uses the forDigit()
method to get the character representation of the digit.
Compile the modified CharForDigit.java
file using the following command:
javac CharForDigit.java
Then, run the code using the following command:
java CharForDigit
You should see the following prompt in the console:
Enter digit:
Enter a digit (e.g. 12) and press Enter. You should see the following prompt:
Enter radix:
Enter a radix (e.g. 16) and press Enter. You should then see the following output:
The character at radix '16' for the digit 12 is: c
This output confirms that the modified code works as expected.
Modify the code to handle invalid user input. Add the following code after the user input code:
} catch (InputMismatchException e) {
System.out.println("Invalid input!");
}
This code catches any InputMismatchException
that is thrown by the Scanner
class when the user enters input that cannot be parsed as an integer.
Compile the modified CharForDigit.java
file using the following command:
javac CharForDigit.java
Then, run the code using the following command:
java CharForDigit
You should see the following prompt in the console:
Enter digit:
Enter a non-numeric input (e.g. "abc") and press Enter. You should see the following output:
Invalid input!
This output confirms that the code handles invalid input as expected.
Modify the code to check if the entered radix is valid. Add the following code after the user input code:
if (r < Character.MIN_RADIX || r > Character.MAX_RADIX) {
System.out.println("Invalid radix!");
return;
}
This code checks if the entered radix is less than the minimum radix or greater than the maximum radix. If so, it prints a message and terminates the program.
Compile the modified CharForDigit.java
file using the following command:
javac CharForDigit.java
Then, run the code using the following command:
java CharForDigit
You should see the following prompt in the console:
Enter digit:
Enter a digit (e.g. 6) and press Enter. You should see the following prompt:
Enter radix:
Enter a radix less than 2
or greater than 36
(e.g. 1
or 100
) and press Enter. You should see the following output:
Invalid radix!
This output confirms that the code handles invalid radix as expected.
In this lab, you learned how to use the forDigit()
method of the Character
class to get the character representation of a digit in a specified radix. You also learned how to handle user input and check for valid input.