In: Computer Science
Write a program that asks the user to enter the name of a file,
and then asks the user to enter a character. The program should
count and display the number of times that the specified character
appears in the file. Use Notepad or another text editor to create a
sample file that can be used to test the program.
Sample Run
java FileLetterCounter
Enter file name: wc4↵
Enter character to count: 0↵
The character '0' appears in the file wc4 17times.↵

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class FileLetterCounter {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = in.nextLine();
System.out.println("Enter character to count: ");
char ch = in.nextLine().charAt(0);
int c, count = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
while ((c = br.read()) != -1) {
if (ch == (char) c) {
count++;
}
}
System.out.println("The character appears in the file " + count + " times.");
in.close();
}
}
please upvote. Thanks!