Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago