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, andScanner. - Input Handling: The program takes input from the user using the
Scannerclass. - HashMap for Storing Frequencies: We use a
HashMapto 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
HashMapusinggetOrDefaultto handle default values. - Displaying the Frequencies: After calculating the frequencies, we display each character’s frequency using a
for-eachloop to iterate through the entries of theMap.
How to Run the Program
To run the program, follow these steps:
-
- Ensure you have Java installed on your computer. If not, download and install Java from the official Oracle website.
- Open a text editor (e.g., Notepad, Sublime Text, VSCode) and copy the Java code into a new file. Save the file with a
.javaextension, for example,FrequencyCounter.java. - Open a terminal or command prompt and navigate to the directory where the Java file is saved.
- Compile the Java program by running the following command:
javac FrequencyCounter.java
-
- Run the compiled program using the following command:
java FrequencyCounter
- The program will prompt you to enter a string, after which it will display the frequency of each character.

