In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.
Example:
The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple method among all others.
// Java program to check if a string
// contains only digits using Character.isDigit()
public class CheckDigits {
public static boolean onlyDigits(String s) {
// Traverse each character in the string
for (int i = 0; i < s.length(); i++) {
// Check if the character is not a digit
if (!Character.isDigit(s.charAt(i))) {
// If any character is not a digit, return false
return false;
}
}
return true; // If all characters are digits, return true
}
public static void main(String[] args) {
System.out.println(onlyDigits("1234"));
System.out.println(onlyDigits("123s"));
}
}
Output
true false
Table of Content
Other Methods to Check if a String Contains only Digits
1. Using Regular Expressions
In this method, we will use the regular expressions to check if the string matches the pattern of only digits. This method is more complex than Character.isDigit() but still very readable and concise.
// Java program to check if a string
// contains only digits using regex
import java.util.regex.*;
public class CheckDigits {
public static boolean onlyDigits(String s) {
// Regex to check if the string
// contains only digits
return s.matches("[0-9]+");
}
public static void main(String[] args) {
System.out.println(onlyDigits("1234"));
System.out.println(onlyDigits("123s"));
}
}
Explanation: In the above example, the regular expression "[0-9]+" matches strings that contain only digits. The matches() method returns true if the string matches the regex.
2. Using Traversal
In this method, 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.
// Java program to check if a string
// contains only digits using traversal
class CheckDigits {
public static boolean onlyDigits(String s, 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 (s.charAt(i) < '0'
|| s.charAt(i) > '9') {
return false;
}
}
// If we reach here, that means
// all characters were digits
return true;
}
public static void main(String args[]) {
String s = "1s234";
int l = s.length();
System.out.println(onlyDigits(s, l));
}
}
Output
false
Note: This approach works but is less readable than Character.isDigit(), as it requires checking ASCII values manually.
3. Using Arraylist.contains() Method
In this method, it creates an ArrayList of digits (0 to 9) and checks if each character in the string is present in this list. It is less efficient and more complex than other methods.
// Java program to check a string
// contains only digits using Arraylist.contains()
import java.util.*;
class CheckDigits {
public static boolean onlyDigits(String s) {
// String containing all digits
String d = "0123456789";
ArrayList<Character> n = new ArrayList<Character>();
// Add all digits to ArrayList
for (int i = 0; i < d.length(); i++) {
n.add(d.charAt(i));
}
// Traverse the string and check
// if each character is a digit
for (int i = 0; i < s.length(); i++) {
// Check if the character is not a digit
if (!n.contains(s.charAt(i))) {
return false;
}
}
// If we reach here,
// all characters are digits
return true;
}
public static void main(String args[]) {
String s = "1234";
System.out.println(onlyDigits(s));
}
}
Output
true