Java Program ASAP ************This program must work in hypergrade and pass all the test cases.********** Please modify this program evem more becausse when it does not pass the test cases as shown in the screenshot. I have provided the correct test case too. In the output Stop and smell the roses.\n and 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 some of the letters need to be lower cased no space after .\n. Also, for the output 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 this sentence Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n need to be in another line. Thank you! Test Case 1 Please enter the file name or type QUIT to exit:\n text1.txtENTER Stop and smell the roses.\n Test Case 2 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 Test Case 3 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 Test Case 4 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; boolean fileNotFound; System.out.print("Please enter the file name or type QUIT to exit:\n"); do{ //while (true) { fileNotFound = false; // Reset fileNotFound flag for each iteration //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(); 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); boolean newSentence = true; 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(); break; } catch (IOException e) { fileNotFound = true; System.out.println("File '" + inputFileName + "' is not found."); System.out.print("Please re-enter the file name or type QUIT to exit:\n"); } //if (fileNotFound) { //System.out.print("Please re-enter the file name or type QUIT to exit:\n"); //} }while(true); } catch (IOException e) { e.printStackTrace(); } } } text1.txt StopAndSmellTheRoses. text2.txt ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit. PleaseExplainABitMoreInTheWayOfFootnotes.Fro
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n some of the letters need to be lower cased no space after .\n. Also, for the output
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n this sentence Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n need to be in another line.
Test Case 1
text1.txtENTER
Stop and smell the roses.\n
Test Case 2
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
Test Case 3
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
Test Case 4
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;
boolean fileNotFound;
System.out.print("Please enter the file name or type QUIT to exit:\n");
do{
//while (true) {
fileNotFound = false; // Reset fileNotFound flag for each iteration
//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();
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);
boolean newSentence = true;
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();
break;
} catch (IOException e) {
fileNotFound = true;
System.out.println("File '" + inputFileName + "' is not found.");
System.out.print("Please re-enter the file name or type QUIT to exit:\n");
}
//if (fileNotFound) {
//System.out.print("Please re-enter the file name or type QUIT to exit:\n");
//}
}while(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
PleaseExplainABitMoreInTheWayOfFootnotes.FromTheGivenTextIt'sNotClearWhatAreWeReadingAbout.
Step by step
Solved in 3 steps