0% found this document useful (0 votes)
4 views1 page

Java Word Frequency Counter Code

The Java program reads a text file named 'sample.txt' and counts the frequency of each word, ignoring case and punctuation. It stores the word counts in a HashMap and prints the results to the console. The program handles potential IOExceptions during file reading.

Uploaded by

vineshbaghel10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Word Frequency Counter Code

The Java program reads a text file named 'sample.txt' and counts the frequency of each word, ignoring case and punctuation. It stores the word counts in a HashMap and prints the results to the console. The program handles potential IOExceptions during file reading.

Uploaded by

vineshbaghel10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;

public class Problem3 {


public static void main(String[] args) {
String fileName = "[Link]";
Map<String, Integer> wordCountMap = new HashMap<>();

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {


String line;

while ((line = [Link]()) != null) {


line = [Link]().replaceAll("[^a-zA-Z0-9\\s]", "");
String[] words = [Link]("\\s+");

for (String word : words) {


if (![Link]()) {
[Link](word, [Link](word, 0) + 1);
}
}
}
} catch (IOException e) {
[Link]("Error reading file: " + [Link]());
}

[Link]("Word Frequency Count:");


[Link]((word, count) -> [Link](word + ": " + count));
}
}

You might also like