m should count and display the number of times that the specified character appears in the file. Class name: FileLetterCounter Hints: Hint 1. To read a single character from the keyboard, you need to read a full line and then take the first character in position 0. Hint 2. To count the occurrences of a given characters in a file you need to read the file line-by-line using a while loop, then for each line check all characters in all positions and increment the character counter for each occurrence. This will require to have a for-loop nested inside the while-loop.   Here is a working code. Modify this code so it will pass the test cases. import java.io.File; import java.io.FileNotFoundEx

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Chapter 4. PC #6. File Letter Counter (page 264)
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.
Class name: FileLetterCounter
Hints:
Hint 1. To read a single character from the keyboard, you need to read a full line and then take the first character in position 0.
Hint 2. To count the occurrences of a given characters in a file you need to read the file line-by-line using a while loop, then for each line check all characters in all positions and increment the character counter for each occurrence. This will require to have a for-loop nested inside the while-loop.
 
Here is a working code. Modify this code so it will pass the test cases.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileRead {
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the file name or type QUIT to exit:");
String filename = keyboard.nextLine();
if (filename.equalsIgnoreCase("Quit"))
return;

File myFile = new File(filename);
while (!myFile.exists()) {
System.out.println("File: " + filename + " does not exist.");
System.out.println("Please enter the file name again or type QUIT to exit:");
filename = keyboard.nextLine();
if (filename.equalsIgnoreCase("Quit")) {
return;
}
myFile = new File(filename);
}

Scanner inputFile = new Scanner(myFile);
int lines = 0;
while (inputFile.hasNext()) {
String str = inputFile.nextLine();
lines++;
System.out.println(lines + ": " + str);
}
inputFile.close();
}
}

Test Case 1

 
 
Please enter the file name or type QUIT to exit:\n
QUITENTER
 

Test Case 2

 
 
Please enter the file name or type QUIT to exit:\n
badfilename.txtENTER
File: badfilename.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
quitENTER
 

Test Case 3

 
 
Please enter the file name or type QUIT to exit:\n
input.txtENTER
Please enter a character: \n
oENTER
Letter 'o' occurs 4 times in the file 'input.txt'.\n
 

Test Case 4

 
 
Please enter the file name or type QUIT to exit:\n
letters.txtENTER
Please enter a character: \n
aENTER
Letter 'a' occurs 82 times in the file 'letters.txt'.\n

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education