Kodekraftt
/Blog
/Count Digits In A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Count Digits in a String in Java — Definition, Examples and Program

Learn how to count digits in a string using Java. This blog explains the logic with examples, a simple Java program, and practice challenges for beginners.

Count Digits in a String in Java — Definition, Examples and Program

Counting digits in a string is a common string-processing task. It helps in validating inputs like phone numbers, IDs, and mixed text data.

In this blog, you will learn:

  • What digits are
  • How to count digits in a string
  • Step-by-step examples
  • A simple Java program
  • Practice challenges

What Are Digits?

Digits are numeric characters from 0 to 9.

How the Logic Works

To count digits in a string:

  1. Read the input string.
  2. Traverse each character of the string.
  3. Check whether the character is between '0' and '9'.
  4. Increase the digit count when a digit is found.

Example 1

Input: "Java123"

  • 1 → digit
  • 2 → digit
  • 3 → digit

Total Digits: 3

Example 2

Input: "Room No 45A"

  • 4 → digit
  • 5 → digit

Total Digits: 2

Java Program to Count Digits


import java.util.Scanner;

public class CountDigits {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String input = sc.nextLine();

        int digitCount = 0;

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            if (ch >= '0' && ch <= '9') {
                digitCount++;
            }
        }

        System.out.println("Number of digits: " + digitCount);
    }
}

Sample Output


Enter a string: hospitalRoom23
Number of digits: 2

Practice Challenges

  1. Modify the program to count letters, digits, and special characters separately.
  2. Check if a string contains only digits.
  3. Extract all digits from a string and form a number.

Counting digits is an essential skill for input validation and string analysis in Java. It forms the basis for many real-world applications.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.