Java Program to Count Occurrences of Words in a String
Introduction
Counting the occurrences of words in a string is a common task in text processing. This exercise helps you understand how to manipulate strings and use data structures like maps in Java to store and count occurrences. This guide will walk you through writing a Java program that counts the occurrences of each word in a given string.
Learn everything about Java: https://www.javaguides.net/
Problem Statement
Create a Java program that:
- Prompts the user to enter a string.
- Splits the string into words.
- Counts the occurrences of each word in the string.
- Displays each word along with its count.
Example:
- Input:
"Java is a programming language and Java is also an island"
- Output:
Java: 2 is: 2 a: 1 programming: 1 language: 1 and: 1 also: 1 an: 1 island: 1
Solution Steps
- Read the String: Use the
Scanner
class to take the string as input from the user. - Split the String into Words: Use the
split()
method to break the string into words. - Store Word Counts: Use a
HashMap
to store each word of the string as keys and their counts as values. - Display the Word Counts: Print each word along with its count.
- Close Resources: Close the
Scanner
class object automatically using the try-resource statement.
Java Program
// Java Program to Count Occurrences of Words in a String
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WordOccurrenceCounter {
public static void main(String[] args) {
// Step 1: Read the string from the user
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Step 2: Split the string into words
String[] words = input.split("\\s+");
// Step 3: Store word counts
Map<String, Integer> wordCountMap = new HashMap<>();
for (String word : words) {
word = word.toLowerCase(); // Normalize to handle case-insensitivity
wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);
}
// Step 4: Display the word counts
System.out.println("Word occurrences:");
for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
Explanation
Step 1: Read the String
- The
Scanner
class is used to read a string input from the user. ThenextLine()
method captures the entire line as a string.
Step 2: Split the String into Words
- The
split()
method is used to divide the string into words based on whitespace. The regex\\s+
handles multiple spaces between words.
Step 3: Store Word Counts
- A
HashMap
is used to store the words as keys and their counts as values. The words are converted to lowercase to handle case-insensitivity.
Step 4: Display the Word Counts
- The program iterates through the
HashMap
entries and prints each word along with its count.
Output
Enter a string: Java is a programming language and Java is also an island
Word occurrences:
java: 2
is: 2
a: 1
programming: 1
language: 1
and: 1
also: 1
an: 1
island: 1
Conclusion
This Java program demonstrates how to count and display the occurrences of words in a user-input string. It covers important concepts such as string manipulation, using maps to store word counts, and iterating through collections, making it a valuable exercise for beginners learning Java programming.
Learn everything about Java: https://www.javaguides.net/