Java Program ASAP ************This program must work in hypergrade and pass all the test cases.********** The text files are located in Hypergrade. Also 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. This program down below does not pass the test cases as shown in the screenshot I have provided the correct test case as a screenshot too. Please modify it or create a new program so it paases the test cases. Thank you! 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(); } } } Chapter 9. PC #14. Word Separator (modified *** Read carefully ***) Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it line by line. While entering the file name, the program should allow the user to type quit to exit the program. If the file with a given name does not exist, then display a message and allow the user to re-enter the file name. The file can contain one or more sentences. The program should accept as input each sentence in which all the words are run together, but the first character of each word is uppercase. Convert sentences to strings in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.” Sentences are separated by periods ('.'), exclamation marks ('!'), or question marks ('?'). Ensure that there is one space character in between sentences. text1.txt StopAndSmellTheRoses. text2.txt ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit. PleaseExplainABitMoreInTheWayOfFootnotes.Fro
text1.txtENTER
Stop and smell the roses.\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
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
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();
}
}
}
PleaseExplainABitMoreInTheWayOfFootnotes.FromTheGivenTextIt'sNotClearWhatAreWeReadingAbout.
Step by step
Solved in 3 steps