Java 8 – How to check whether given String contains only Digits ?

In this article, we will discuss how to check whether the given String contains only Digits using Java 8 Stream

1. Check String contains only Digits :

  1. Using Java 8 Stream.allMatch() and Character.isDigit(ch) methods
  2. Using for-loop and Character.isDigit(ch) method

1.1 Using Java 8 Stream.allMatch() and Character.isDigit(ch) methods

  • First, check whether the given String is not null and not empty otherwise return false
  • Once after the given String passes above validation then
    • get Stream using CharSequence.chars() method
    • validate each characters iterated by passing to Character.isDigit(ch) method to check whether passed character is Digit only
  • Finally, invoke this method with different Strings like
    • null
    • only digits
    • empty string
    • space
    • only letters
    • alphanumeric
    • digits with spaces in between
    • only special characters
    • Combo of alphabets and special characters
  • On the basis of boolean return-value from the method, print passed String contains only digits (true) or not (false)

CheckStringOnlyDigitsUsingJava8.java

package in.bench.resources.string.operation;

public class CheckStringOnlyDigitsUsingJava8 {

	// main() method
	public static void main(String[] args) {

		// print to console
		System.out.println("1. Whether null is only digits :- " 
				+ isDigits(null));
		System.out.println("2. Whether \"811\" is only digits :- " 
				+ isDigits("811"));
		System.out.println("3. Whether empty string (\"\") is only digits :- " 
				+ isDigits(""));
		System.out.println("4. Whether empty space (\" \") is only digits :- " 
				+ isDigits(" "));
		System.out.println("5. Whether \"Bench\" is only digits :- " 
				+ isDigits("Bench"));
		System.out.println("6. Whether \"Bench123\" is only digits :- " 
				+ isDigits("Bench123"));
		System.out.println("7. Whether \"123_456\" is only digits :- " 
				+ isDigits("123_456"));
		System.out.println("8. Whether \"!@#$%^&*()_+\" is only digits :- " 
				+ isDigits("!@#$%^&*()_+"));
		System.out.println("9. Whether \"986709\" is only digits :- " 
				+ isDigits("986709"));
		System.out.println("10. Whether \"123$\" is only digits :- " 
				+ isDigits("123$"));
	}


	/**
	 * This method is used to check whether passed String contains digits only
	 * using Java 8 Stream
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigits(String str) {

		// check for null & empty
		if(str == null || str.isEmpty() || str.length() == 0) {

			// return false
			return false;
		}


		// return if its all match
		return str.chars().allMatch(ch -> Character.isDigit(ch));
	}
}

Output:

1. Whether null is only digits :- false
2. Whether "811" is only digits :- true
3. Whether empty string ("") is only digits :- false
4. Whether empty space (" ") is only digits :- false
5. Whether "Bench" is only digits :- false
6. Whether "Bench123" is only digits :- false
7. Whether "123_456" is only digits :- false
8. Whether "!@#$%^&*()_+" is only digits :- false
9. Whether "986709" is only digits :- true
10. Whether "123$" is only digits :- false

1.2 Using for-loop and Character.isDigit(ch) method

  • First, check whether the given String is not null and not empty otherwise return false
  • Once after the given String passes above validation, iterate using for-loop starting from index 0 till length of the String
    • get character one-by-one using String.charAt(index) method
    • validate each characters iterated by passing to Character.isDigit(ch) method to check whether passed character is Digit only
  • Finally, invoke this method with different Strings like
    • null
    • only digits
    • empty string
    • space
    • only letters
    • alphanumeric
    • digits with spaces in between
    • only special characters
    • Combo of alphabets and special characters
  • On the basis of boolean return-value from the method, print passed String contains only digits (true) or not (false)

CheckStringContainsOnlyDigits.java

package in.bench.resources.string.operation;

public class CheckStringContainsOnlyDigits {

	// main() method
	public static void main(String[] args) {

		// print to console
		System.out.println("1. Whether null is only digits :- " 
				+ isDigits(null));
		System.out.println("2. Whether \"811\" is only digits :- " 
				+ isDigits("811"));
		System.out.println("3. Whether empty string (\"\") is only digits :- " 
				+ isDigits(""));
		System.out.println("4. Whether empty space (\" \") is only digits :- " 
				+ isDigits(" "));
		System.out.println("5. Whether \"Bench\" is only digits :- " 
				+ isDigits("Bench"));
		System.out.println("6. Whether \"Bench123\" is only digits :- " 
				+ isDigits("Bench123"));
		System.out.println("7. Whether \"123_456\" is only digits :- " 
				+ isDigits("123_456"));
		System.out.println("8. Whether \"!@#$%^&*()_+\" is only digits :- " 
				+ isDigits("!@#$%^&*()_+"));
		System.out.println("9. Whether \"986709\" is only digits :- " 
				+ isDigits("986709"));
		System.out.println("10. Whether \"123$\" is only digits :- " 
				+ isDigits("123$"));
	}


	/**
	 * This method is used to check whether passed String contains digits only
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigits(String str) {

		// check for null & empty
		if(str == null || str.isEmpty() || str.length() == 0) {

			// return false
			return false;
		}


		// iterate through String - reading one character at a time
		for(int index = 0; index < str.length(); index++) {


			// get one character at a time
			char ch = str.charAt(index);


			// check passed character is digit
			if(!Character.isDigit(ch)) {

				// return false
				return false;
			}
		}

		// otherwise, return true
		return true;
	}
}

Output:

1. Whether null is only digits :- false
2. Whether "811" is only digits :- true
3. Whether empty string ("") is only digits :- false
4. Whether empty space (" ") is only digits :- false
5. Whether "Bench" is only digits :- false
6. Whether "Bench123" is only digits :- false
7. Whether "123_456" is only digits :- false
8. Whether "!@#$%^&*()_+" is only digits :- false
9. Whether "986709" is only digits :- true
10. Whether "123$" is only digits :- false

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether given String contains Alphanumeric ?
Java 8 – How to check whether given String contains only Alphabets or Letters ?