Please Modify this program ASAP BECAUSE IT IS HOMEWORK ASSIGNMENT so it passes all the test cases. It does not pass the test cases when I upload it to Hypergrade. Also, for test cases 1-4 it wants only to input Please enter the file name or type QUIT to exit: then input the file and display the Total number of words. For test cases 5 and 7 it wants to only to input Please enter the file name or type QUIT to exit then input input5.txt and then display File: input5.txt does not exist.\n then display Please enter the file name again or type QUIT to exit:\n then type input1.txt to display the total number of words or type quit to exit the program. For test case 6 it wants only to Please enter the file name again or type QUIT to exit:\n then type quit to exit the porgram.
Because RIGHT NOW IT PASSES 1 OUT OF 7 TEST CASES. I have provided the failed the test cases as a screenshot. The program must pass the test case when uploaded to Hypergrade.
Because RIGHT NOW IT PASSES 1 OUT OF 7 TEST CASES. I have provided the failed the test cases as a screenshot. The program must pass the test case when uploaded to Hypergrade.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String fileName;
while (true) {
System.out.println("Please enter the file name or type QUIT to exit:");
fileName = keyboard.nextLine().trim();
if (fileName.equalsIgnoreCase("QUIT")) {
break; // Exit the loop when "QUIT" is entered
}
File file = new File(fileName);
if (file.exists()) {
int wordCount = countWords(file);
System.out.println("Total number of words: " + wordCount);
} else {
System.out.println("File: " + fileName + " does not exist.");
}
}
keyboard.close();
}
public static int countWords(File file) {
int wordCount = 0;
try {
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
// Remove leading and trailing whitespaces
String line = inputFile.nextLine().trim();
if (!line.isEmpty()) {
String[] words = line.split("\s+");
wordCount += words.length;
}
}
inputFile.close();
} catch (FileNotFoundException e) {
// File existence already validated
// catch exception for completeness
e.printStackTrace();
}
return wordCount;
}
}
input1.txt
this is a test
input2.txt
this
is
a
test
input3.txt
Empty
input4txt
this this
is is
a a
test test
Test Case 1
input1.txtENTER
Total number of words: 4\n
Test Case 2
input2.txtENTER
Total number of words: 4\n
Test Case 3
input3.txtENTER
Total number of words: 0\n
Test Case 4
input4.txtENTER
Total number of words: 8\n
Test Case 5
input5.txtENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
input1.txtENTER
Total number of words: 4\n
Test Case 6
qUitENTER
Test Case 7
input5.txtENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
quItENTER
![Test Case 1 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input1.txt ENTER
Total number of words: 4\n
Please e... OUTPUT TOO LONG
Test Case 2 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input2.txt ENTER
Total number of words: 4\n
Please e... OUTPUT TOO LONG
Test Case 3 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input3.txt ENTER
Total number of words: 0\n
Please e... OUTPUT TOO LONG
Test Case 4 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input4.txt ENTER
Total number of words: 8\n
Please e... OUTPUT TOO LONG
Test Case 5 Failed
Please enter the file name or type QUIT to exit: \n
input5.txt ENTER
Show what's missing
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
Please enter the file name or type QUIT to exit:\n
input... OUTPUT TOO LONG
Test Case 6 Passed!
Please enter the file name or type QUIT to exit: \n
qUit ENTER
Test Case 7 Failed
Show what's missing
Please enter the file name or type QUIT to exit: \n
input5.txt ENTER
File: input5.txt does not exist.\n|
Please enter the file name again or type QUIT to exit: \n
Please enter the fil... OUTPUT TOO LONG](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F35042b75-f2f6-4aa6-80ce-f24554663c41%2F37aa26c8-3f75-4e69-89a8-6224b7a97b89%2F0ep938e_processed.png&w=3840&q=75)
![Test Case 1 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input1.txt ENTER
Total number of words: 4\n
Test Case 2 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input2.txt ENTER
Total number of words: 4\n
Test Case 3 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input3.txt ENTER
Total number of words: 0\n
Test Case 4 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input4.txt ENTER
Total number of words: 8\n
Test Case 5 Failed
Show what's missing
Please enter the file name or type QUIT to exit: \n
input5.txt ENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
input1.txt ENTER
Total number of words: 4\n
Test Case 6 Passed!
Please enter the file name or type QUIT to exit: \n
qUit ENTER
Test Case 7 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
input5.txt ENTER
File: input5.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
quIt ENTER](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F35042b75-f2f6-4aa6-80ce-f24554663c41%2F37aa26c8-3f75-4e69-89a8-6224b7a97b89%2Fmguefc8_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 5 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)