Java

 

 

Introduction

In programming, counting the frequency of each character in a string is a common task. This operation helps in analyzing the occurrence of characters and can be used in many applications such as cryptography, text analysis, and data processing. The goal of this program is to count the frequency of each character in a given string and display the results.

Objective

The objective of this Java program is to:

  • Accept a string input from the user.
  • Count the frequency of each character in the string.
  • Display the frequency of each character in the string.

Java Code

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

public class FrequencyCounter {

    public static void main(String[] args) {
        // Create a Scanner object to read input from user
        Scanner scanner = new Scanner(System.in);

        // Ask the user for a string input
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Create a HashMap to store character frequencies
        Map<Character, Integer> frequencyMap = new HashMap<>();

        // Iterate over each character in the string
        for (char ch : input.toCharArray()) {
            // Update the frequency count for the character
            frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
        }

        // Display the frequency of each character
        System.out.println("\nCharacter Frequencies:");
        for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Close the scanner
        scanner.close();
    }
}

Explanation of the Program Structure

This Java program consists of the following key sections:

  • Import Statements: We import necessary classes such as HashMap, Map, and Scanner.
  • Input Handling: The program takes input from the user using the Scanner class.
  • HashMap for Storing Frequencies: We use a HashMap to store the frequency of each character in the string. The key is the character, and the value is its frequency count.
  • Looping through the String: The program iterates over each character in the input string. For each character, it updates its frequency in the HashMap using getOrDefault to handle default values.
  • Displaying the Frequencies: After calculating the frequencies, we display each character’s frequency using a for-each loop to iterate through the entries of the Map.

How to Run the Program

To run the program, follow these steps:

    1. Ensure you have Java installed on your computer. If not, download and install Java from the official Oracle website.
    2. Open a text editor (e.g., Notepad, Sublime Text, VSCode) and copy the Java code into a new file. Save the file with a .java extension, for example, FrequencyCounter.java.
    3. Open a terminal or command prompt and navigate to the directory where the Java file is saved.
    4. Compile the Java program by running the following command:
javac FrequencyCounter.java
    1. Run the compiled program using the following command:
java FrequencyCounter
  1. The program will prompt you to enter a string, after which it will display the frequency of each character.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)