ava Program ASAP   ************This program must work in hypergrade and pass all the test cases.**********   For test case 2 and 4 when the file does not exist repeat Please re-enter the file name or type QUIT to exit:\n after the test file is not found.  The text files are located in Hypergrade.        For test case 1 it wants only Please enter the file name or type QUIT to exit:\n text1.txtENTER Stop and smell the roses.\n   For test case 2 it wants only  Please enter the file name or type QUIT to exit:\n txt1.txtENTER File 'txt1.txt' is not found.\n Please re-enter the file name or type QUIT to exit:\n text1.txtENTER Stop and smell the roses.\n   For test case 3 it wants only  Please enter the file name or type QUIT to exit:\n text2.txtENTER A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n   For test case 4 it wants only Please enter the file name or type QUIT to exit:\n somefile.txtENTER File 'somefile.txt' is not found.\n Please re-enter the file name or type QUIT to exit:\n anotherbadfile.txtENTER File 'anotherbadfile.txt' is not found.\n Please re-enter the file name or type QUIT to exit:\n quitENTER   import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Pattern; import java.util.regex.Matcher; public class WordSeparator {     public static void main(String[] args) {         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {             String inputFileName;             while (true) {                 System.out.print("Please enter the file name or type QUIT to exit:\n");                 inputFileName = reader.readLine();                 if (inputFileName.equalsIgnoreCase("QUIT")) {                     break;                 }                 try {                     BufferedReader fileReader = new BufferedReader(new FileReader(inputFileName));                     String line;                     StringBuilder result = new StringBuilder();                     boolean newSentence = true; // Flag to handle sentence separation                     while ((line = fileReader.readLine()) != null) {                         // Use regular expressions to split the input into words and punctuation                         Pattern pattern = Pattern.compile("([A-Z][a-z]*|[.!?])");                         Matcher matcher = pattern.matcher(line);                         while (matcher.find()) {                             String token = matcher.group();                             if (newSentence && !token.equals(".")) {                                 result.append(token.substring(0, 1).toUpperCase()).append(token.substring(1));                                 newSentence = false;                             } else {                                 if (token.equals(".") || token.equals("!") || token.equals("?")) {                                     newSentence = true;                                 }                                 result.append(" ").append(token);                             }                         }                     }                     System.out.println(result.toString().trim()); // Remove trailing newline                     fileReader.close();                 } catch (IOException e) {                     System.out.println("File '" + inputFileName + "' is not found.");                 }             }         } catch (IOException e) {             e.printStackTrace();         }     } }     text1.txt StopAndSmellTheRoses.   text2.txt ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit. PleaseExplainABitMoreInTheWayOfFootnotes.FromTheGivenTextIt'sNotClearWhatAreWeReadingAbout.

Programming with Microsoft Visual Basic 2017
8th Edition
ISBN:9781337102124
Author:Diane Zak
Publisher:Diane Zak
Chapter9: Sequential Access Files And Menus
Section: Chapter Questions
Problem 11E
icon
Related questions
Question
Java Program ASAP
 
************This program must work in hypergrade and pass all the test cases.**********
 
For test case 2 and 4 when the file does not exist repeat Please re-enter the file name or type QUIT to exit:\n after the test file is not found.  The text files are located in Hypergrade. 
 
 
 
For test case 1 it wants only
Please enter the file name or type QUIT to exit:\n
text1.txtENTER
Stop and smell the roses.\n
 
For test case 2 it wants only 
Please enter the file name or type QUIT to exit:\n
txt1.txtENTER
File 'txt1.txt' is not found.\n
Please re-enter the file name or type QUIT to exit:\n
text1.txtENTER
Stop and smell the roses.\n
 
For test case 3 it wants only 
Please enter the file name or type QUIT to exit:\n
text2.txtENTER
A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n
 
For test case 4 it wants only
Please enter the file name or type QUIT to exit:\n
somefile.txtENTER
File 'somefile.txt' is not found.\n
Please re-enter the file name or type QUIT to exit:\n
anotherbadfile.txtENTER
File 'anotherbadfile.txt' is not found.\n
Please re-enter the file name or type QUIT to exit:\n
quitENTER
 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class WordSeparator {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
            String inputFileName;
            while (true) {
                System.out.print("Please enter the file name or type QUIT to exit:\n");
                inputFileName = reader.readLine();
                if (inputFileName.equalsIgnoreCase("QUIT")) {
                    break;
                }

                try {
                    BufferedReader fileReader = new BufferedReader(new FileReader(inputFileName));
                    String line;
                    StringBuilder result = new StringBuilder();

                    boolean newSentence = true; // Flag to handle sentence separation

                    while ((line = fileReader.readLine()) != null) {
                        // Use regular expressions to split the input into words and punctuation
                        Pattern pattern = Pattern.compile("([A-Z][a-z]*|[.!?])");
                        Matcher matcher = pattern.matcher(line);

                        while (matcher.find()) {
                            String token = matcher.group();

                            if (newSentence && !token.equals(".")) {
                                result.append(token.substring(0, 1).toUpperCase()).append(token.substring(1));
                                newSentence = false;
                            } else {
                                if (token.equals(".") || token.equals("!") || token.equals("?")) {
                                    newSentence = true;
                                }
                                result.append(" ").append(token);
                            }
                        }
                    }

                    System.out.println(result.toString().trim()); // Remove trailing newline
                    fileReader.close();
                } catch (IOException e) {
                    System.out.println("File '" + inputFileName + "' is not found.");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 
 
text1.txt
StopAndSmellTheRoses.
 
text2.txt
ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit.
PleaseExplainABitMoreInTheWayOfFootnotes.FromTheGivenTextIt'sNotClearWhatAreWeReadingAbout.
Test Case 1 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop And Smell The Roses \n
Please OUTPUT TOO LONG
Test Case 2 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
txt1.txt ENTER
File txt1.txt' is not found.\n
Please enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop And Smell The Roses .\n
Please enter the fi... OUTPUT TOO LONG
Test Case 3 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
text2.txt ENTER
A True Rebel You Are !Everyone Was Impressed .You Do Well To Continue In The Same Spirit .Please Explain A Bit More In The Way Of Footnotes From The Given Text It Not Clear What Are We Readin
g About .\n
Please enter the file name or ... OUTPUT TOO LONG
Test Case 4 Failed
Show what's missing
Please enter the file name or type QUIT to exit: \n
somefile.txt ENTER
File 'some file.txt' is not found.
Please enter the file name or type QUIT to exit: \n
anotherbadfile.txt ENTER
File 'anotherbadfile.txt' is not found.\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 text1.txt ENTER Stop And Smell The Roses \n Please OUTPUT TOO LONG Test Case 2 Failed Show what's missing Please enter the file name or type QUIT to exit: \n txt1.txt ENTER File txt1.txt' is not found.\n Please enter the file name or type QUIT to exit: \n text1.txt ENTER Stop And Smell The Roses .\n Please enter the fi... OUTPUT TOO LONG Test Case 3 Failed Show what's missing Please enter the file name or type QUIT to exit: \n text2.txt ENTER A True Rebel You Are !Everyone Was Impressed .You Do Well To Continue In The Same Spirit .Please Explain A Bit More In The Way Of Footnotes From The Given Text It Not Clear What Are We Readin g About .\n Please enter the file name or ... OUTPUT TOO LONG Test Case 4 Failed Show what's missing Please enter the file name or type QUIT to exit: \n somefile.txt ENTER File 'some file.txt' is not found. Please enter the file name or type QUIT to exit: \n anotherbadfile.txt ENTER File 'anotherbadfile.txt' is not found.\n Please enter the file name or type QUIT to exit: \n quit ENTER
Test Case 1
Please enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop and smell the roses. \n
Test Case 2
Please enter the file name or type QUIT to exit: \n
txt1.txt ENTER
File 'txt1.txt' is not found. \n
Please re-enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop and smell the roses.\n
Test Case 3
Please enter the file name or type QUIT to exit: \n
text2.txt ENTER
A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n
Test Case 4
Please enter the file name or type QUIT to exit: \n
somefile.txt ENTER
File 'somefile.txt' is not found.\n
Please re-enter the file name or type QUIT to exit: \n
anotherbadfile.txt ENTER
File 'anotherbadfile.txt' is not found. \n
Please re-enter the file name or type QUIT to exit: \n
quit ENTER
Transcribed Image Text:Test Case 1 Please enter the file name or type QUIT to exit: \n text1.txt ENTER Stop and smell the roses. \n Test Case 2 Please enter the file name or type QUIT to exit: \n txt1.txt ENTER File 'txt1.txt' is not found. \n Please re-enter the file name or type QUIT to exit: \n text1.txt ENTER Stop and smell the roses.\n Test Case 3 Please enter the file name or type QUIT to exit: \n text2.txt ENTER A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n Test Case 4 Please enter the file name or type QUIT to exit: \n somefile.txt ENTER File 'somefile.txt' is not found.\n Please re-enter the file name or type QUIT to exit: \n anotherbadfile.txt ENTER File 'anotherbadfile.txt' is not found. \n Please re-enter the file name or type QUIT to exit: \n quit ENTER
Expert Solution
steps

Step by step

Solved in 4 steps with 6 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
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
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
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage