** Java expert please help me JAVA program below? Here is the requirement: Pleas
ID: 3805331 • Letter: #
Question
** Java expert please help me JAVA program below? Here is the requirement:
Please show your program's output as example above
---------CountOccurrenceOfWords.java---------
import java.util.*;
public class CountOccurrenceOfWords {
public static void main(String[] args) {
// Set text in a string
String text = "Good morning. Have a good class. " +
"Have a good visit. Have fun!";
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<String, Integer>();
String[] words = text.split("[ .,;:!?(){}]");
for (int i = 0; i < words.length; i++) {
String key = words[i].toLowerCase();
if (key.length() > 0) {
if (!map.containsKey(key)) {
map.put(key, 1);
}
else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
}
// Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// Get key and value from each entry
for (Map.Entry<String, Integer> entry: entrySet)
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
Explanation / Answer
Answer:
import java.util.*;
public class CWordTest {
public static void main(String[] args) {
CWordTest read = new CWordTest();
read.infunc("Practice makes man perfect");
}
public void infunc(String value) {
Map <String, String> map = new HashMap <String, String> ();
if (value != null) {
String[] param = value.split(" ");
for (String str: param) {
if (map.containsKey(str)) {
int incv = Integer.parseInt(map.get(str));
map.put(str, String.valueOf(incv + 1));
} else {
map.put(str, "1");
}
}
}
System.out.println(" Count value " + map);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.