-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString6.java
More file actions
37 lines (24 loc) · 956 Bytes
/
String6.java
File metadata and controls
37 lines (24 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
public class String6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
text = text.toLowerCase();
text = text.replaceAll("[^a-z0-9\\s]", " ");
String[] words = text.trim().split("\\s+");
HashMap<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
if (!word.isEmpty()) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
}
List<String> sortedWords = new ArrayList<>(wordCount.keySet());
Collections.sort(sortedWords);
System.out.println("Word Frequency Report:");
for (String word : sortedWords) {
System.out.println(word + ": " + wordCount.get(word));
}
System.out.println("Total Unique Words: " + wordCount.size());
scanner.close();
}
}