**** Java expert please help me JAVA program below? Here is the requirement: Ple
ID: 3806149 • 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
HI, Please find my implementation.
Please let me know in case of any issue.
import java.io.File;
import java.util.*;
public class CountOccurrenceOfWords {
public static void main(String[] args) {
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<String, Integer>();
Scanner sc = new Scanner(System.in);
try{
if(args == null ){
System.out.println("Please pass input file name from command line");
return;
}
String inputFileName = args[0];
System.out.println("Input file name: "+args[0]);
// opening file
Scanner fileScanner = new Scanner(new File(inputFileName));
String line;
while(fileScanner.hasNextLine()){
line = fileScanner.nextLine();
String[] words = line.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);
}
}
}
}
sc.close();
fileScanner.close();
}catch (Exception e) {
System.out.println(e.getMessage());
}
// Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//getting max count
int max = 0;
for (Map.Entry<String, Integer> entry: entrySet){
if(entry.getValue() > max)
max = entry.getValue();
}
String result = "";
// Get key and value from each entry
for (Map.Entry<String, Integer> entry: entrySet)
if(entry.getValue() == max)
result = result + entry.getKey()+" ";
System.out.println("Words that appears "+max+" times: "+result);
}
}
/*
Sample run:
Input file name: text.txt
Words that appears 2 times: and are dachshunds dogs
*/
######### text.txt #########
Sophie Sally and jack were dachshunds
Dachshunds are the best dogs and all dogs
are better than cats
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.