Check Whether a String Contains Only Digits
Return true only if the string is non-empty and every character is a digit from 0 to 9.
Scan the string once. If it is empty, return false. For each character, check whether it lies between '0' and '9'; the moment you find one that does not, return false. If the loop finishes without a non-digit, return true.
Problem Statement
Given a string, determine whether it consists only of decimal digits
('0' through '9'). Return true if every character is a digit and the
string is not empty; otherwise return false.
A leading +/-, a decimal point, or any letter or space makes the answer
false. For example, "12345" returns true, while "12a45" and "-99"
return false.
Input: A single string s.
Output: A boolean: true if s is non-empty and all characters are digits, else false.
Examples
Input: "12345"
Output: trueEvery character is between '0' and '9', and the string is not empty.
Input: "12a45"
Output: falseThe character 'a' is not a digit, so the whole check fails.
Constraints
0 <= s.length() <= 10000Only characters '0'-'9' count as digits; no sign or decimal point allowed
Think Before You Code
Reveal the questions to ask yourself first
- How do you test whether a single character is a digit without a library call?
- As soon as you find one non-digit, do you need to keep scanning?
- What should an empty string return, and why?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Do a single left-to-right pass with an early exit.
- If the string is empty, return
false. - For each character
c:- If
c < '0'orc > '9', returnfalse.
- If
- If the loop completes, every character was a digit, so return
true.
Comparing against the characters '0' and '9' works because the digit
characters are contiguous and in order in ASCII.
Dry Run
Walk through the example step by step
Checking "12a45":
index | char | '0'<=c<='9'? | action
------+------+--------------+------------------
0 | 1 | yes | continue
1 | 2 | yes | continue
2 | a | no | return false
result: false
Solution
Reveal the full Java solution
public class OnlyDigits {
public static boolean isAllDigits(String s) {
if (s.isEmpty()) {
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isAllDigits("12345")); // true
System.out.println(isAllDigits("12a45")); // false
}
}
The check succeeds only if the loop never triggers an early return false,
which means every character passed the '0'-to-'9' range test. The explicit
empty-string guard reflects the common convention that "only digits" implies at
least one digit is present.
Returning as soon as a non-digit is seen keeps the work at worst O(n) and often far less, since a single bad character ends the scan immediately.
O(n) where n is the length of the stringSpace: O(1)Common Mistakes
- Returning true for an empty string when the requirement is at least one digit.
- Accepting signs or a decimal point (like "-99" or "3.14") as numeric when only 0-9 are allowed.
Edge Cases to Test
- The empty string returns false.
- A single-digit string like "7" returns true.
- A string with a trailing space, such as "123 ", returns false.
Interview Follow-Ups
- How would you extend this to allow an optional leading sign and one decimal point?
- How would using Character.isDigit change behaviour for non-ASCII digit characters?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →