JAVA PPROGRAM ASAP   Hypergrade does not like this program because it says 2 out of 7 passed and take out the extra \n from the program. Please Modify and change this program ASAP BECAUSE IT IS HOMEWORK ASSIGNMENT so it passes all the test cases. The program must pass the test case when uploaded 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.    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;                 System.out.println("Please enter the file name or type QUIT to exit:");         fileName = keyboard.nextLine().trim();         while (!fileName.equalsIgnoreCase("QUIT")) {             File file = new File(fileName);             if (file.exists()) {                 int wordCount = countWords(file);                 System.out.println("Total number of words: " + wordCount + "\n");                 break;  // Exit after successful word count             } else {                 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().trim();             }         }         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;     } }   Chapter 9. PC #10. Word Counter (page 610) Write a program that asks the user for the name of a file. The program should display the number of words that the file contains. Input Validation: Make sure the file exists, before proceeding. Let the user type quit in order to exit the program. If the file is empty, the word count should be 0.   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

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
JAVA PPROGRAM ASAP
 
Hypergrade does not like this program because it says 2 out of 7 passed and take out the extra \n from the program. Please Modify and change this program ASAP BECAUSE IT IS HOMEWORK ASSIGNMENT so it passes all the test cases. The program must pass the test case when uploaded 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. 
 

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;
       
        System.out.println("Please enter the file name or type QUIT to exit:");
        fileName = keyboard.nextLine().trim();

        while (!fileName.equalsIgnoreCase("QUIT")) {
            File file = new File(fileName);
            if (file.exists()) {
                int wordCount = countWords(file);
                System.out.println("Total number of words: " + wordCount + "\n");
                break;  // Exit after successful word count
            } else {
                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().trim();
            }
        }
        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;
    }
}

 
Chapter 9. PC #10. Word Counter (page 610)
Write a program that asks the user for the name of a file. The program should display the number of words that the file contains.
Input Validation:
Make sure the file exists, before proceeding. Let the user type quit in order to exit the program. If the file is empty, the word count should be 0.
 

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
inputs.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 Passed!
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 inputs.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 Passed! 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
\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
\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
\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
\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
inputl.txt ENTER
Total number of words: 4\n
\n
Test Case 6 Passed!
Please enter the file name or type QUIT to exit: \n
qUit ENTER
Test Case 7 Passed!
Please enter the file name or type QUIT to exit: \n
inputs.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 \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 \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 \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 \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 inputl.txt ENTER Total number of words: 4\n \n Test Case 6 Passed! Please enter the file name or type QUIT to exit: \n qUit ENTER Test Case 7 Passed! Please enter the file name or type QUIT to exit: \n inputs.txt ENTER File: input5.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n quIt ENTER
Expert Solution
steps

Step by step

Solved in 4 steps with 8 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
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