-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseventeen.java
More file actions
25 lines (23 loc) · 829 Bytes
/
seventeen.java
File metadata and controls
25 lines (23 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class seventeen{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sentence:");
String input = scanner.nextLine();
input = input.toLowerCase();
String[] words = input.split("\\W+");
Map<String, Integer> wordMap = new HashMap<>();
for (String word : words) {
if (!word.isEmpty()) {
wordMap.put(word, wordMap.getOrDefault(word, 0) + 1);
}
}
System.out.println("Word occurrences:");
for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
scanner.close();
}
}