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 Please modify it so it passes the test cases because I run it it does not work:  mport 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

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter13: File Input And Output
Section: Chapter Questions
Problem 10PE
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 Please modify it so it passes the test cases because I run it it does not work: 

mport 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 4 steps with 5 images

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
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage