Character isDigit() method in Java with examples

  1. The java.lang.Character.isDigit(char ch) is an inbuilt method in java which determines whether a specified character is a digit or not. There are few conditions that a character must accomplish to be accepted as a digit. That is if the general category type of a character, provided by Character.getType(ch), is DECIMAL_DIGIT_NUMBER, then the character is a digit.
    Some Unicode character ranges that contain digits:

    From ‘\u0030’ to ‘\u0039’ : ISO-LATIN-1 digits (‘0’ through ‘9’)
    From ‘\u0660’ to ‘\u0669’ : Arabic-Indic digits
    From ‘\u06F0’ to ‘\u06F9’ : Extended Arabic-Indic digits
    From ‘\u0966’ to ‘\u096F’ : Devanagari digits
    From ‘\uFF10’ to ‘\uFF19’ : Fullwidth digits

    Apart from the above mentioned ranges, many other character ranges contain digits as well.

    Syntax:

    public static boolean isDigit(char ch)
    

    Parameter: This method accepts character parameter ch as an argument, which is to be tested.



    Return value:This method returns a boolean value. It returns True if ch is digit, else False.

    Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isDigit(int) method.

    Below programs illustrate the above method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate the
    // Character.isDigit() method
      
    import java.util.*;
    import java.lang.*;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // two characters
            char c1 = 'A', c2 = '4';
      
            // Function to check if the character
            // is digit or not
            System.out.println(
                c1 + " is a digit -> "
                + Character.isDigit(c1));
            System.out.println(
                c2 + " is a digit -> "
                + Character.isDigit(c2));
        }
    }

    chevron_right

    
    

    Output:

    A is a digit -> false
    4 is a digit -> true
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate the
    // Character.isDigit() method
      
    import java.util.*;
    import java.lang.*;
      
    public class GFG {
      
        public static int search_digit(String s)
        {
      
            // Function to check if is digit
            // is found or not
            for (int i = 0; i < s.length(); i++) {
                if (Character.isDigit(
                        s.charAt(i))
                    == true) {
                    // return position of digit
                    return i + 1;
                }
            }
            // return 0 if digit not present
            return 0;
        }
      
        public static void main(String[] args)
        {
      
            // Array of strings
            String[] arr = { "ABC4DEF", "QWERTY" };
      
            // To store the position of digit
            int index = 0;
      
            // Traverse the array arr[] to find digit
            // within it's elements
            for (String x : arr) {
                index = search_digit(x);
                if (index != 0) {
                    System.out.println(
                        "Digit found at : "
                        + (index)
                        + "th position.");
                }
                else {
                    System.out.println(
                        "Digit not present.");
                }
            }
        }
    }

    chevron_right

    
    

    Output:

    Digit found at : 4th position.
    Digit not present.
    
  2. The java.lang.Character.isDigit(int codePoint) is an inbuilt method in java which determines whether the specified Unicode code point character of integer type is a digit or not.
    There are few conditions that a character must accomplish to be accepted as a digit. That is if the general category type of a character, provided by getType(codepoint), is a DECIMAL_DIGIT_NUMBER, then the character is a digit. Some Unicode character ranges that contain digits:

    From ‘\u0030’ to ‘\u0039’ : ISO-LATIN-1 digits (‘0’ through ‘9’)
    From ‘\u0660’ to ‘\u0669’ : Arabic-Indic digits
    From ‘\u06F0’ to ‘\u06F9’ : Extended Arabic-Indic digits
    From ‘\u0966’ to ‘\u096F’ : Devanagari digits
    From ‘\uFF10’ to ‘\uFF19’ : Fullwidth digits



    Apart from the above mentioned ranges, many other character ranges contain digits as well.

    Syntax:

    public static boolean isDigit(int codePoint)
    

    Parameter: This method accepts unicode character parameter codePoint of integer type as an argument, which is to be tested.

    Return value: This method returns a boolean value. It returns True if the specified character is digit, else it returns False.

    Below programs illustrate the above method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This program demonstrates the use of
    // isDigit(int codePoint) method of Character class.
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create codePoints
            int cp1 = 57;
            int cp2 = 84;
      
            // Check whether the codePoints
            // are digit or not.
            System.out.println(
                "The codePoint cp1 is a digit -> "
                + Character.isDigit(cp1));
            System.out.println(
                "The codePoint cp2 is a digit -> "
                + Character.isDigit(cp2));
        }
    }

    chevron_right

    
    

    Output:

    The codePoint cp1 is a digit -> true
    The codePoint cp2 is a digit -> false
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This program demonstrates the use of
    // isDigit(int codePoint) method of
    // Character class.
    import java.util.*;
      
    public class Main {
        public static void main(String[] args)
        {
            // create codePoints
            int cp1 = 0x50;
            int cp2 = 0x06f8;
      
            // Check whether the codePoints
            // are digit or not.
            System.out.println(
                "The codePoint cp1 is a digit -> "
                + Character.isDigit(cp1));
            System.out.println(
                "The codePoint cp2 is a digit -> "
                + Character.isDigit(cp2));
        }
    }

    chevron_right

    
    

    Output:

    The codePoint cp1 is a digit -> false
    The codePoint cp2 is a digit -> true
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-char-

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.