Check if string contains only digits in Java



Write a program that checks if the given string contains only digits.

We show three ways to check if the given string contains only digits:

  1. Character.isDigit() solution
  2. String.matches() solution
  3. Java 8, functional-style solution

Java program to Check if contains only digits

public final class Strings {

    public static boolean containsOnlyDigitsV1(String str) {

        if (str == null || str.isBlank()) {
            // or throw IllegalArgumentException
            return false;
        }
        
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        
        return true;
    }

    public static boolean containsOnlyDigitsV2(String str) {

        if (str == null || str.isBlank()) {
            // or throw IllegalArgumentException
            return false;
        }

        return str.matches("[0-9]+");
    }

    public static boolean containsOnlyDigitsV3(String str) {

        if (str == null || str.isBlank()) {
            // or throw IllegalArgumentException
            return false;
        }

        return !str.chars()
                .anyMatch(n -> !Character.isDigit(n));
    }
}

Test

public class Main {

    private static final String ONLY_DIGITS = "123456789";
    
    private static final String NOT_ONLY_DIGITS = "123456789A";
    
    public static void main(String[] args) {

        System.out.println("Input text with only digits: \n" + ONLY_DIGITS + "\n");
        System.out.println("Input text with other characters: \n" + NOT_ONLY_DIGITS + "\n");
                
        System.out.println("Character.isDigit() solution:");
        
        boolean onlyDigitsV11 = Strings.containsOnlyDigitsV1(ONLY_DIGITS);
        boolean onlyDigitsV12 = Strings.containsOnlyDigitsV1(NOT_ONLY_DIGITS);
        System.out.println("Contains only digits: " + onlyDigitsV11);
        System.out.println("Contains only digits: " + onlyDigitsV12);
        
        System.out.println();        
        System.out.println("String.matches() solution:");
        
        boolean onlyDigitsV21 = Strings.containsOnlyDigitsV2(ONLY_DIGITS);
        boolean onlyDigitsV22 = Strings.containsOnlyDigitsV2(NOT_ONLY_DIGITS);
        
        System.out.println("Contains only digits: " + onlyDigitsV21);
        System.out.println("Contains only digits: " + onlyDigitsV22);
        
        System.out.println();                
        System.out.println("Java 8, functional-style solution:");
        
        boolean onlyDigitsV31 = Strings.containsOnlyDigitsV3(ONLY_DIGITS);
        boolean onlyDigitsV32 = Strings.containsOnlyDigitsV3(NOT_ONLY_DIGITS);
        System.out.println("Contains only digits: " + onlyDigitsV31);
        System.out.println("Contains only digits: " + onlyDigitsV32);        
    }
}

Output:

Input text with only digits: 
123456789

Input text with other characters: 
123456789A

Character.isDigit() solution:
Contains only digits: true
Contains only digits: false

String.matches() solution:
Contains only digits: true
Contains only digits: false

Java 8, functional-style solution:
Contains only digits: true
Contains only digits: false

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments