How to check if string contains only digits in Java

05 Jul, 2022
Given string str, the task is to write a Java program to check whether a string contains only digits or not. If so, then print true, otherwise false. 
Examples: 
Input: str = “1234” Output: true Explanation: The given string contains only digits so that output is true.
Input: str = “GeeksforGeeks2020” Output: false Explanation: The given string contains alphabet character and digits so that output is false.  
The idea is to traverse each character in the string and check if the character of the string contains only digits from 0 to 9. If all the character of the string contains only digits then return true, otherwise, return false. Below is the implementation of the above approach: 
Java
// Java program for the above approach// contains only digitsclass GFG {     // Function to check if a string    // contains only digits    public static boolean    onlyDigits(String str, int n)    {        // Traverse the string from        // start to end        for (int i = 0; i < n; i++) {             // Check if character is            // not a digit between 0-9            // then return false            if (str.charAt(i) < '0'                || str.charAt(i) > '9') {                return false;            }        }          // If we reach here, that means          // all characters were digits.        return true;    }     // Driver Code    public static void main(String args[])    {        // Given string str        String str = "1a234";        int len = str.length();         // Function Call        System.out.println(onlyDigits(str, len));    }}
false
Time Complexity: O(N), where N is the length of the given string. 
Auxiliary Space: O(1)  
The idea is to iterate over each character of the string and check whether the specified character is a digit or not using Character.isDigit(char ch). If the character is a digit then return true, else return false. Below is the implementation of the above approach:  
Java
// Java program to check if a string// contains only digitsclass GFG {     // Function to check if a string    // contains only digits    public static boolean onlyDigits(String str, int n)    {         // Traverse the string from        // start to end        for (int i = 0; i < n; i++) {             // Check if the sepecified            // character is a not digit            // then return false,            // else return false            if (!Character.isDigit(str.charAt(i))) {                return false;            }        }          // If we reach here that means all          // the characters were digits,          // so we return true        return true;    }     // Driver Code    public static void main(String args[])    {        // Given string str        String str = "1234";        int len = str.length();         // Function Call        System.out.println(onlyDigits(str, len));    }}
true
Time Complexity: O(N), where N is the length of the given string. 
Auxiliary Space: O(1) 
Get the String. 
Create a Regular Expression to check string contains only digits as mentioned below:  
regex = "[0-9]+";
Match the given string with Regular Expression. In Java, this can be done by using Pattern.matcher().
Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
Java
// Java program to check if a string// contains only digitsimport java.util.regex.*;class GFG {     // Function to validate URL    // using regular expression    public static boolean    onlyDigits(String str)    {        // Regex to check string        // contains only digits        String regex = "[0-9]+";         // Compile the ReGex        Pattern p = Pattern.compile(regex);         // If the string is empty        // return false        if (str == null) {            return false;        }         // Find match between given string        // and regular expression        // using Pattern.matcher()        Matcher m = p.matcher(str);         // Return if the string        // matched the ReGex        return m.matches();    }     // Driver Code    public static void main(String args[])    {        // Given string str        String str = "1234";         // Function Call        System.out.println(onlyDigits(str));    }}
true
Time Complexity: O(N), where N is the length of the given string. 
Auxiliary Space: O(1)  
sinanitw
Nikhil Arya 1
simmytarika5
Java-Character
java-regular-expression
Java-String-Programs
number-digits
regular-expression
Java Programs
Pattern Searching
School Programming
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.