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.
Solutions
1. 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
class GFG {
public static boolean
onlyDigits(String str, int n)
{
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) < '0'
|| str.charAt(i) > '9' ) {
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String str = "1a234" ;
int len = str.length();
System.out.println(onlyDigits(str, len));
}
}
|
- Time Complexity: O(N), where N is the length of the given string.
- Auxiliary Space: O(1)
2. 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 a digit then return true, else return false. Below is the implementation of the above approach:
Java
class GFG {
public static boolean onlyDigits(String str, int n)
{
for ( int i = 0 ; i < n; i++) {
if (!Character.isDigit(str.charAt(i))) {
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String str = "1234" ;
int len = str.length();
System.out.println(onlyDigits(str, len));
}
}
|
- Time Complexity: O(N), where N is the length of the given string.
- Auxiliary Space: O(1)
3. 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
import java.util.regex.*;
class GFG {
public static boolean
onlyDigits(String str)
{
String regex = "[0-9]+" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str = "1234" ;
System.out.println(onlyDigits(str));
}
}
|
- Time Complexity: O(N), where N is the length of the given string.
- Auxiliary Space: O(1)
4. Using contains() method and ArrayList
Java
import java.lang.*;
import java.util.*;
class Main{
public static boolean onlyDigits(String str, int n)
{
String num= "0123456789" ;
ArrayList<Character> numbers = new ArrayList<Character>();
for ( int i= 0 ;i<num.length();i++)
{
numbers.add(num.charAt(i));
}
for ( int i = 0 ; i < n; i++) {
if (!numbers.contains(str.charAt(i))) {
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String str = "1a234" ;
int len = str.length();
System.out.println(onlyDigits(str, len));
}
}
|