Java Program to Count the Occurrences of Each Character

Understanding the frequency of characters in a text can be essential for various reasons, such as text analysis, cryptography, or even just for fun. In this article, we'll walk beginners through creating a Java program that counts the occurrences of each character in a given string. 

Overview

The idea is simple. We will go through each character in the string and keep a count of how many times each character appears. 

We use HashMap to store the character along with it's count.

Java Program to Count the Occurrences of Each Character

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CharacterCount {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the string:");
        String input = scanner.nextLine();

        Map<Character, Integer> charCount = countCharacters(input);

        System.out.println("Occurrences of each character:");
        for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    public static Map<Character, Integer> countCharacters(String input) {
        Map<Character, Integer> charCountMap = new HashMap<>();

        for (char c : input.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }

        return charCountMap;
    }
}

Output:

Enter the string:
hello
Occurrences of each character:
h: 1
e: 1
l: 2
o: 1

Step by Step Explanation: 

Setting up the Input: 
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the string:");
        String input = scanner.nextLine();
Using the Scanner class, we take the string input from the user. 

Using a HashMap:
        Map<Character, Integer> charCount = countCharacters(input);
We employ a HashMap where the key is the character from the string and the value is the count of that character. The HashMap allows for quick look-ups and insertions.

Counting Characters:
        for (char c : input.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }
We convert the string to a character array and iterate through each character. For every character, we update its count in our HashMap. If the character doesn't exist, we use a default value of 0 and add 1 to it. 

Displaying the Results: 
        System.out.println("Occurrences of each character:");
        for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
We iterate through the entries of our HashMap and print out the character and its corresponding count.

Conclusion

Counting the occurrences of characters in a string is a foundational exercise in understanding data structures like HashMap and text manipulation in Java. In this article, we explored how to write a Java program to count the occurrences of each character.

Related Java String Programs with Output

Comments