Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1 10 pointsl: Write a program that asks a user for a file name and prin

ID: 3902812 • Letter: E

Question

Exercise 1 10 pointsl: Write a program that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it prints the frequency of each word (case insensitive) containing in the file (Test1.txt); Your program should handle exceptions related to file opening and reading from the file. This is a test this Hi there hi hi hi Output at the console: Number of characters: 35 Number of words 10 Number of lines: 2 Data written to the file (Testl.txt): this 2 is 1 1 test 1 hi 4 there 1 Hint: you may use a HashMap to store and count the frequency of the words in the file.

Explanation / Answer

import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class FileStats { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); String filename = in.nextLine(); try { Scanner fin = new Scanner(new File(filename)); int lineCount = 0, wordCount = 0, characterCount = 0; String line, words[]; HashMap map = new HashMap(); while (fin.hasNextLine()) { line = fin.nextLine(); words = line.split(" "); lineCount++; wordCount += words.length; for(int i = 0; i