Question: import java.io.BufferedReader; import java.io.FileReader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; public class CountWordInstances { public static void main(String[] args) throws Exception { Path

import java.io.BufferedReader; import java.io.FileReader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map;

public class CountWordInstances {

public static void main(String[] args) throws Exception {

Path path = Paths.get(System.getProperty("user.dir")) .resolve("rnek.txt");

BufferedReader reader = new BufferedReader(new FileReader(path.toFile()));

Map frequency = new HashMap<>();

String line = reader.readLine(); while (line != null) { // System.out.println("Processing line: " + line);

if (!line.trim().equals("")) { String[] words = line.split(" ");

for (String word : words) { if (word == null || word.trim().equals("")) { continue; } String processed = word.toLowerCase(); processed = processed.replace(",", "");

if (frequency.containsKey(processed)) { frequency.put(processed, frequency.get(processed) + 1); } else { frequency.put(processed, 1); } } }

line = reader.readLine(); }

System.out.println(frequency);

int mostFrequentlyUsed = 0; String theWord = null;

for (String word : frequency.keySet()) { Integer theVal = frequency.get(word); if (theVal > mostFrequentlyUsed) { mostFrequentlyUsed = theVal; theWord = word; } } System.out.println(); System.out.printf("the most frequently used word is '%s', %d times", theWord, mostFrequentlyUsed);

}

}Writing this code without changing its function by changing some places

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!