The format in which data is being provided by the end-user is very critical in performing several tasks. If the user provides an age in integers, then mathematical operation over the received value can be done easily. That’s why various data validation techniques must be applied to retrieve the most authentic data.
One of these techniques when it comes to identifying the occurrence of numerical values in the provided String is “java.lang.Character.isDigit()”. This is a built-in method in Java that determines whether the selected character or the Unicode value represents a numerical digit or not.
This guide explains the usage of a character isDigit() method in Java.
How to Use the Java Character isDigit() Method?
The “Character.isDigit()” method has two versions depending on the provided parameters. If the provided parameter is a single character type value. Then, the “isDigit()” method considers it as a single entity and identifies if the provided value is a numerical digit or not. The syntax is shown below:
public static boolean isDigit(char selCh)
In the above syntax, the “selCh” is an argument variable storing the value which is going to be checked for a numerical digit. If the provided argument is a digit the “isDigit()” method will return “true” or else return false.
The other variation of an “isDigit()” method accepts integer values that are converted into their corresponding Unicode or ASCII. If the value retrieved after the conversion is a numerical digit then, the value of “true” will be returned, and “false” in the other case. Its syntax is shown below:
public static boolean isDigit(int codePoint)
The “codePoint” is an argument that will be converted into the corresponding Unicode characters. If the converted character is a numerical digit, only then the value of “true” is returned.
Let’s dive into the working of a Java character “isDigit()” method.
Example 1: Identifying Character is a Digit or Not
In this example, the existence of numerical digit in two provided character data type values is being identified using the “isDigit()” method:
import java.util.*;
public class javabeat {
public static void main(String[] args)
{
char val1 = 'M', val2 = '7';
System.out.println( "Is provided value " + val1 + " is a digit: " + Character.isDigit(val1) );
System.out.println( "Is provided value " + val2 + " is a digit: " + Character.isDigit(val2) );
}
}
The explanation of the above code is as follows:
- First, the public class named “javabeat” is created that contains the “main()” method. Inside the main() method, declare and initialize two variables named “val1” and “val2”.
- Next, pass the “val1” and “val2” as arguments to the separate “Character.isDigit()” methods. The results provided by these methods are displayed on the console to provide information if the provided variables contain digits as their values or not.
The generated output shows that the char type variable containing the digit value has been identified:
Example 2: Retrieving The Position of Digit in Array Elements
In this example, the array elements are checked in a sequential manner to find the occurrence of a numerical digit. Once found, the position of a numerical digit in that string gets printed over the console, as shown below:
public class javabeat {
public static void main(String[] args) {
String[] provArray = { "JAVA89BEAT", "JavaBeat", "B6eatJ8va"};
int arrayIndex = 0;
for (String x : provArray) {
arrayIndex = search_digit(x);
if (arrayIndex != 0) {
System.out.println("Digit found from provided String at: " + (arrayIndex) + "th position.");
}
else {
System.out.println("Provided array element does not contain any Digit!");
}
}
}
public static int digitFinder(String provString) {
for (int x = 0; x < provString.length(); x++) {
if (Character.isDigit(provString.charAt(x)) == true) {
return x + 1;
}
}
return 0;
}
}
The description of the above code is as follows:
- First, the class “javabeat” having the “main()” method is created and it contains the targeted array named “provArray”. This array contains three elements, two of them contain a couple of numerical digits and the remaining one contains no numerical digits.
- Next, the integer type “arrayIndex” variable is declared as having the initial value of “0”.
- Moreover, the enhanced “for” loop is utilized to iterate over the “provArray” elements and pass each element as an argument for the “digitFinder()” function.
- Now, preserve the result of a “digitFinder()” function in the “arrayIndex” variable. This variable contains information about the occurrence of digits and their position.
- Next, the “if” statement is used to check if the “arrayIndex” variable is empty or not. If the variable is not empty then, the data about numerical digits is displayed on the console. If the variable is empty means there is not any occurrence of a numerical digit.
- After that, define a “digitFinder()” function that accepts a single parameter of “provString” or a single element of the array.
- Then, the “for” loop is utilized that iterates till the length of a provided String array element “provString” starts from “0”.
- Inside it, the “if” statement is used that contains the “Character.isDigit()” method, and each character of a String is passed into the method. If the result of this method returns true, then the value of “x” is incremented to store the exact position of the numerical digit starting from “1”.
- Finally, if the result is “false” then return “0” and exit the “for” loop.
The generated output shows the position of numerical digits in each array element retrieved via the “isDigit()” method:
Example 3: Use of “isDigit()” Method With ASCII CodePoints Value
In this example, instead of “char” type variables the “int” type variables storing codepoint values are utilized. The provided values are however numerical by appearance but they are converted into corresponding “ASCII Characters” and the one that has ASCII numerical characters are considered as numerical digits by the “isDigit()” method. The code is mentioned below:
public class javabeat {
public static void main(String[] args) {
int codePoint1 = 57;
int codePoint2 = 84;
System.out.println("Is Provided codePoint of '" + codePoint1 + "' a digit: " + Character.isDigit(codePoint1));
System.out.println( "Is Provided codePoint of '" + codePoint2 + "' a digit: " + Character.isDigit(codePoint2));
}
}
In the above code block:
- First, the public scope class “javabeat” containing the “main()” method is created. Inside the method, two “int” type variables along with random values are declared and initialized.
- These two variables are then passed as an argument for the “isDigit()” method separately to identify the occurrence of a numeric digit variable.
The below output shows that the “codePoint1” variable stores numeric digits and not the “codePoint2” variable:
Example 4: Use of “isDigit()” Method With Unicode CodePoints Value
In this case, the code points that are being used are in Unicode format to verify the working of the “isDigit()” method with Unicode values. The code snippet is shown below:
public class javabeat {
public static void main(String[] args) {
int codePoint1 = 0x06f1;
int codePoint2 = 0x09f1;
System.out.println("\nIs Provided codePoint of '0x06f1' a digit: " + Character.isDigit(codePoint1));
System.out.println("Is Provided codePoint of '0x09f3' a digit: " + Character.isDigit(codePoint2));
}
}
In the above code, two hexadecimal values “0x06f1” and “0x09f1” are being used and passed as an argument for the “isDigit()” method. This method checks which one of these values is a digit and prints the result in boolean format accordingly.
The generated output shows that the value of “true” is returned for the digits:
Example 5: Use of Character.isDigit() Method to Decode a Run-length Encoded String in Java
The “Character.isDigit()” method can be utilized to decode the String that is encoded in RLE(Run-length Encoded) format. The “RLE” is a form of lossless compression where the number of repetitions of a single character in the string is stated next to the character. For instance, “g3” means to enter three times “g” in sequence.
Algorithm
- The “Character.isDigit()” method is applied over the String.
- If the first or selected character of the String is not a digit, then that character is stored as the “currentCharacter”.
- Then the second character from the String is selected. If the character is a digit, then the current character is displayed on the console according to that digit.
- After that, the third String character is passed, and so on the process continues till all characters of the String are traversed.
Let’s implement the discussed algorithm in a Java program. To decode a Run-length encoded String using the “Character.isDigit()” method. The code is provided below:
public class javabeat {
public static void main(String[] args) {
String provString = "j2a3v4a5b5e4a3t2";
String output = "";
int noOfTimes = 0;
char currentCharacter = provString.charAt(0);
for (int x = 1; x < provString.length(); x++) {
// If the digit is found, append current Characters noOfTimes to Output
if (Character.isDigit(provString.charAt(x))) {
// Calculating the noOfTimes
noOfTimes = noOfTimes * 10 + Character.digit(provString.charAt(x), 10);
do { // Appending it to Output to generate decoded String
output += currentCharacter;
noOfTimes--;
}
while (noOfTimes > 0);
} else { noOfTimes = 0;
currentCharacter = provString.charAt(x);
}
}
System.out.println(output);
}
}
The description of the above code is written below:
- In the “main()” method, declare the “provString”, “output”, and “noOfTimes” variables. Then, initialize these variables with the encoded String, empty String, and “0” integer, respectively.
- Define another variable “currentCharacter” and assign it the first character of a desired String using the “charAt()” method.
- Next, execute the “for” loop until the length of String, and inside it, utilize the “if” statement to check if the current character is a digit or not using the “Character.isDigit()” method.
- If the current character is not a digit, then the current String character is stored in the “currentCharacter” named variable.
- If the current String character is a “digit” then, the number of counts will be calculated. The calculated result is stored in the “noOfTimes” variables
- After that, utilize the “do while” loop that iterates until “noOfTimes” gets “0”. Inside the loop, the “currentCharacter” gets appended in the “output” variable till “noOfTimes” values. After each append reduce the “noOfTimes” variable value by one factor.
- Finally, display the “output” variable over the console that contains the encoded “Run-length” string.
The output shows that the encoded Run-length String has been decoded and displayed over the console:
This guide has explained the working and usage of Java’s “Character.isDigit()” method.
Conclusion
To use the “Character.isDigit()” method in Java, pass the “char” variable as an argument to check if the variable holds a digit as the value or not. The Unicode or ASCII numerical values can also be passed as the parameters to check whether the provided value when converted to decimal format contains a single-digit value or not. The “Character.isDigit()” method can also be used to decode the encoded “Run-length” string.