JAVA PPROGRAM ASAP 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. 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 Please enter the file name or type QUIT to exit:\n input1.txtENTER Total number of words: 4\n Test Case 2 Please enter the file name or type QUIT to exit:\n input2.txtENTER Total number of words: 4\n Test Case 3 Please enter the file name or type QUIT to exit:\n input3.txtENTER Total number of words: 0\n Test Case 4 Please enter the file name or type QUIT to exit:\n input4.txtENTER Total number of words: 8\n Test Case 5 Please enter the file name or type QUIT to exit:\n 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 Please enter the file name or type QUIT to exit:\n qUitENTER Test Case 7 Please enter the file name or type QUIT to exit:\n input5.txtENTER File: input5.txt does not exist.\n Please enter the file name again or type QUIT to exit:\n quItENTER

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 28PE
icon
Related questions
Question
JAVA PPROGRAM ASAP
 
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. 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

 
 
Please enter the file name or type QUIT to exit:\n
input1.txtENTER
Total number of words: 4\n
 

Test Case 2

 
 
Please enter the file name or type QUIT to exit:\n
input2.txtENTER
Total number of words: 4\n
 

Test Case 3

 
 
Please enter the file name or type QUIT to exit:\n
input3.txtENTER
Total number of words: 0\n
 

Test Case 4

 
 
Please enter the file name or type QUIT to exit:\n
input4.txtENTER
Total number of words: 8\n
 

Test Case 5

 
 
Please enter the file name or type QUIT to exit:\n
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

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

Test Case 7

Please enter the file name or type QUIT to exit:\n
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
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
Transcribed Image Text: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
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 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 or type QUIT to exit: \n
input1.txt ENTER
Total number of words: 4\n
Please enter the file na... 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 or type QUIT to exit: \n
quIt ENTER
Transcribed Image Text: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 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 or type QUIT to exit: \n input1.txt ENTER Total number of words: 4\n Please enter the file na... 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 or type QUIT to exit: \n quIt ENTER
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
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