In previous post we learnt how to find Count number of words in a string java
In this post we will learn how to count total number of times each character appears in the string in java
Here we are going to use map provided by Java collection framework. Logic is to read one character at a time from the string and put it in map character as key, count as value (Initial count value will be 1).
With every character that is read from the String check in the map, if it already exists as a key or not. If it exists then increment the count otherwise add the key in map with initial count value as 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.kkjavatutorials.client; import java.util.HashMap; import java.util.Map; import java.util.Set; /** *@author KK JavaTutorials *This Java program is about counting the total number of times each character * occurs in the given input String */ public class CountCharsInStringTest { public static void main(String[] args) { //Check base conditions by uncomment below calls one by one.. //countCharactersInString(null); //countCharactersInString(""); //countCharactersInString(" "); //Check by passing a valid string.. countCharactersInString("Visit my website kkjavatutorials.com to lean java"); } // This method is used to count characters in a input String private static void countCharactersInString(String inputText){ //Validating base case for null or empty string if(inputText == null || inputText.trim().isEmpty()){ System.out.println("Please enter valid string."); return; } Map<Character, Integer> charCountMap = new HashMap<Character, Integer>(); for(int i = 0; i < inputText.length(); i++){ //taking one character at a time char c = inputText.charAt(i); //we don't need to count spaces if(c == ' ') continue; // If that character is already present there in the map then increase the value by 1 if(charCountMap.containsKey(c)){ charCountMap.put(c, charCountMap.get(c) + 1); }else{ //otherwise put that character in the map with the initial count as 1 charCountMap.put(c, 1); } } //Displaying Character and corresponding count from map Set<Map.Entry<Character, Integer>> charCountEntrySet = charCountMap.entrySet(); for(Map.Entry<Character, Integer> entry : charCountEntrySet){ System.out.println("Character:" + entry.getKey() + " Count:" + entry.getValue()); } } } |
If you run above program in your favorite IDE then you will get blow output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Character:a Count:6 Character:b Count:1 Character:c Count:1 Character:e Count:3 Character:i Count:4 Character:j Count:2 Character:k Count:2 Character:l Count:2 Character:m Count:2 Character:. Count:1 Character:n Count:1 Character:o Count:3 Character:r Count:1 Character:s Count:3 Character:t Count:5 Character:u Count:1 Character:V Count:1 Character:v Count:2 Character:w Count:1 Character:y Count:1 |
You May Also Like:
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
That’s all about How to count total number of times each character appears in the string in java
If you have any feedback or suggestion please feel free to drop in blow comment box.