Given a 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.
- Using Traversal: 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 program for the above approach// contains only digitsclassGFG {// Function to check if a string// contains only digitspublicstaticbooleanonlyDigits(String str,intn){// Traverse the string from// start to endfor(inti =0; i < n; i++) {// Check if character is// digit from 0-9// then return true// else falseif(str.charAt(i) >='0'&& str.charAt(i) <='9') {returntrue;}else{returnfalse;}}returnfalse;}// Driver Codepublicstaticvoidmain(String args[]){// Given string strString str ="1234";intlen = str.length();// Function CallSystem.out.println(onlyDigits(str, len));}}chevron_rightfilter_noneOutput:
true
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1) - Using Character.isDigit(char ch): 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 digit then return true, else return false.
Below is the implementation of the above approach:
// Java program to check if a string// contains only digitsclassGFG {// Function to check if a string// contains only digitspublicstaticbooleanonlyDigits(String str,intn){// Traverse the string from// start to endfor(inti =0; i < n; i++) {// Check if the sepecified// character is a digit then// return true,// else return falseif(Character.isDigit(str.charAt(i))) {returntrue;}else{returnfalse;}}returnfalse;}// Driver Codepublicstaticvoidmain(String args[]){// Given string strString str ="1234";intlen = str.length();// Function CallSystem.out.println(onlyDigits(str, len));}}chevron_rightfilter_noneOutput:true
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1) -
Using Regular Expression:
- 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 program to check if a string// contains only digitsimportjava.util.regex.*;classGFG {// Function to validate URL// using regular expressionpublicstaticbooleanonlyDigits(String str){// Regex to check string// contains only digitsString regex ="[0-9]+";// Compile the ReGexPattern p = Pattern.compile(regex);// If the string is empty// return falseif(str ==null) {returnfalse;}// Find match between given string// and regular expression// using Pattern.matcher()Matcher m = p.matcher(str);// Return if the string// matched the ReGexreturnm.matches();}// Driver Codepublicstaticvoidmain(String args[]){// Given string strString str ="1234";// Function CallSystem.out.println(onlyDigits(str));}}chevron_rightfilter_noneOutput:true
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if given string contains all the digits
- Java Program to Check if all digits of a number divide it
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Check if a string contains only alphabets in Java
- Check if a given string is Pangram in Java
- Check for string rotation in Java
- Program to check if the String is Empty in Java
- Java program to check whether a string is a Palindrome
- Program to check if a String in Java contains only whitespaces
- Check if a String starts with any of the given prefixes in Java
- Check if a string contains only alphabets in Java using Regex
- Program to check if the String is Null in Java
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check if N contains all digits as K in base B
- Check if the given decimal number has 0 and 1 digits only
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.

