Java Program to Count Number of Digits in a String Last Updated : 02 Nov, 2022 Comments Improve Suggest changes Like Article Like Report The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password is : 1234" Output: Total number of Digits = 4 Input : string = "G e e k s f o r G e e k 1234" Output: Total number of Digits = 4Approach: Create one integer variable and initialize it with 0.Start string traversal.If the ASCII code of character at the current index is greater than or equals to 48 and less than or equals to 57 then increment the variable.After the end of the traversal, print variable.Below is the implementation of the above approach: Java // Java Program to Count Number of Digits in a String public class GFG { public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; int digits = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= 48 && str.charAt(i) <= 57) digits++; } System.out.println("Total number of Digits = " + digits); } } OutputTotal number of Digits = 4 Time Complexity: O(N), where N is the length of the string. Approach : Using isDigit() method Java // Java Program to Count Number of Digits in a String public class GFG { public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; int digits = 0; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) digits++; } System.out.println("Total number of Digits = " + digits); } } OutputTotal number of Digits = 4 Approach : Using contains() method and ArrayList Java // Java Program to Count Number of Digits in a String import java.lang.*; import java.util.*; public class Main{ public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; String num="0123456789"; ArrayList<Character> numbers= new ArrayList<>(); for(int i=0;i<num.length();i++) { numbers.add(num.charAt(i)); } int digits = 0; for (int i = 0; i < str.length(); i++) { if (numbers.contains(str.charAt(i))) digits++; } System.out.println("Total number of Digits = "+ digits); } } OutputTotal number of Digits = 4 Comment More infoAdvertise with us Next Article Java Program to Count Number of Digits in a String chetanjha888 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-String-Programs +1 More Practice Tags : Java Similar Reads Java Program to Count Number of Vowels in a String In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io pac 4 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read Java Program to Convert String to Long To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a 3 min read How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin 2 min read How to Check if a String Contains only Digits in Java? 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 3 min read Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s 4 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Count of numbers from range [L, R] that end with any of the given digits Given a range [L, R], the task is to find the count of numbers from the range whose last digit is either 2, 3 or 9.Examples: Input: L = 1, R = 3 Output: 2 2 and 3 are the only valid numbers.Input: L = 11, R = 33 Output: 8 Approach: Initialize a counter count = 0 and run a loop for every element from 4 min read How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Like