Java isDigit method

Published in the Java Developer group
Methods that return a boolean value most often begin with the word “is” and mean checking if the element being checked matches a certain condition. The Character.isDigit() method, that we are going to discuss in this article, determines whether the specified char value is a digit.

Java isDigit method syntax

java.lang.Character.isDigit(char ch) is a built-in method in Java that determines whether the specified character is a digit or not. What does “a digit” mean in the Java programming context? According to the definition in Java Doc, if the Character.getType(ch) method returns DECIMAL_DIGIT_NUMBER constant, then the character is a digit. Some Unicode character ranges that contain digits are the next:
  • From ‘\u0030’ through ‘\u0039’ are ISO-LATIN-1 digits (‘0’ through ‘9’)

  • From ‘\u0660’ through ‘\u0669’ are Arabic-Indic digits

  • From ‘\u06F0’ through‘\u06F9’ are Extended Arabic-Indic digits

  • From ‘\u0966’ through ‘\u096F’ are Devanagari digits

  • From \uFF10' through '\uFF19'are Fullwidth digits

There are also some other ranges containing digits. However, most often we are going to use digits from ‘\u0030’ to ‘\u0039’. The syntax of Character.isDigit() is here:

public static boolean isDigit(char myChar)
Where myChar is the character to be tested. This method returns true if the character is a digit and false otherwise. According to the Java doc isDigit(char myChar) method can’t handle supplementary characters. To support all Unicode characters, including supplementary characters, the programmer should use the isDigit(int) method. It looks the same way, but, thanks to OOP and polymorphism support works a little bit differently. public static boolean isDigit(int codePoint) determines if the specified character (Unicode code point) is a digit. In character encoding terminology, a code point or code position is a numeric value that corresponds to a specific character. isDigit(int codePoint) also returns true if the character is a digit and false otherwise.

Java isDigit method simple example

Let’s try to work with the Java Characher.isDigit() method. First of all we are going to write a simple program to demonstrate the method.

public class isDigitTest {
//isDigit(char ch) simple example
   public static void main(String[] args)
   {

       //characters to check using isDigit Java method
       char myChar1 = '5', myChar2 = 'u', myChar3 = '0';

       // Function to check if the character
       // is digit or not, using isDigit() method
       System.out.println("is char " + myChar1 + " a digit? "
                       + Character.isDigit(myChar1));
       System.out.println(
               "is char " + myChar2 + " a digit? "
                       + Character.isDigit(myChar2));

       System.out.println(
               "is char " + myChar3 + " a digit? "
                       + Character.isDigit(myChar3));
   }
}
The output is:
is char 5 a digit? true is char u a digit? false is char u a digit? true

Java isDigit method, a little bit more complicated example

Let's try to use Character.isDigit() in a more interesting and real life problem. There is a compression method called Run Length Encoding or RLE for short. Run Length Encoding is a data compression algorithm that replaces repeated characters (series) with one character and the number of its repetitions. A series is a sequence consisting of several identical characters. When encoding (packing, compressing), a string of identical characters that make up a series is replaced by a string containing the repeating character itself and the number of its repetitions. So if you have a string “hhhhhorrribleeee” run length encoding gives a result: h5or3ible5. If you decode the string, you should sequentially check if you have a digit or non-digit character, and if you have a digit, it's time to get what the digit is. By the way, all of you know JPEG files. This format uses a variant of run-length encoding in a diagonal pattern over quantized data. The variant is that only the length of runs of zero values are encoded and all other values are encoded as themselves. In the example below, we use the Character.isDigit(char ch) method to decode a string encoded with Run-length encoding. If you are interested, try to complete the program, or rather, create its first part and write a method for RLE encoding a string, as well as reading a string from a file. Or manually entering a string from the console while checking the correctness of the input. Here is an example of RLE decoding:

public class RleTest {

   //the method to decode String using run-length encoding and 
//isDigit() method 
   private static String decodeRle(String encoded) {
       if (encoded.isBlank()) return "";
       StringBuilder result = new StringBuilder();
       int count = 0;
       char baseChar = encoded.charAt(0);
       for (int i = 1; i <= encoded.length(); i++) {
           char c = i == encoded.length() ? '$' : encoded.charAt(i);
//checking using isDigit() method           
if (Character.isDigit(c)) {
               count = count * 10 + Character.digit(c, 10);
           } else {
               do {
                   result.append(baseChar);
                   count--;
               } while (count > 0);
               count = 0;
               baseChar = c;
           }
       }
       return result.toString();
   }
   
public static void main(String[] args) {
//here we are going to decode an RLE-encoded string 
       System.out.println(decodeRle("C3ecddf8"));
   }
}
The output is:
CCCecddffffffff
You may have noticed that we are not using a String, but a StringBuilder. Why? The fact is that String is immutable, and we increment the counter, and each time a new string will be created. We also used the Character.digit method in the program. java.lang.Character.digit() is a method that returns the numeric value of the character ch in the specified number system. If the base is not in the range MIN_RADIX <= base <= MAX_RADIX, or if ch is not a valid digit in the specified base, the method returns -1. Java isDigit method - 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION