Write a program that checks a text file for s all the text from the input file t
ID: 3533185 • Letter: W
Question
Write a program that checks a text file for s all the text from the input file to the output file, but with the following two changes (1) Any string of two or more blank characters is replaced by a single blank; (2) all sentences start with an uppercase letter. All sentences after the first one begin after either a period, a question mark, or an exclamation mark that is followed by one or more white space characters.several formatting and punctuation matters. Also if a line ends with a '.' or a '?' the line is moved to the next line.
input textin.txt:
This program Process. text files and creates! a new file?with * the following.
output textout.txt
This program process.
Text Files and creates!
A new file? with the following.
Explanation / Answer
Please Ignore my above code :
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TextReader {
private static String readFileAsString(String filePath) throws IOException
{
StringBuffer fileData = new StringBuffer();
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
public static void main(String[] args) throws IOException
{
String readData=readFileAsString("E:/1.txt");
readData=readData.replaceAll("[\s]+", " ");
readData=readData.replaceAll("[.][\s]", ". ");
readData=readData.replaceAll("[?][\s]", "? ");
readData=readData.replaceAll("[!][\s]", "! ");
String[] lines=readData.split(" ");
try {
File file = new File("E:/2.txt");
// if file doesnt exists, then create it
if (!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(String e:lines)
{
e=e.replace(e.substring(0, 1), e.substring(0, 1).toUpperCase());
bw.write(e);
bw.newLine();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.