I am also looking for a solution to programming project #1 in chapter 10. Here i
ID: 3858955 • Letter: I
Question
I am also looking for a solution to programming project #1 in chapter 10. Here is the problem:
Write a program that checks a text file for several formatting and punctuation matters. The program asks for the names of both an input and output file. It then copies 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 replaces by a single blank; (2) all sentences start with an uppercase letter. All sentences after the first one begin after the first one begin after either a period, a question mark, or an exclamation mark that is followeed by one or more whitespace characters.
Explanation / Answer
StringOperations.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class StringOperations {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String text = "";
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please enter the input file ");
String inputFile = in.next();
System.out.println("Please enter output file");
String outputFile = in.next();
File input = new File(inputFile);
BufferedReader br = new BufferedReader(new FileReader(input));
String sCurrentLine;
if(input.exists()){
while ((sCurrentLine = br.readLine()) != null) {
text = text + sCurrentLine + " ";
}
System.out.println("-----------Input file content--------------");
System.out.println(text);
text = text.replaceAll("\s+", " ").trim();
int pos = 0;
boolean capitalize = true;
StringBuilder sb = new StringBuilder(text);
while (pos < sb.length()) {
if (sb.charAt(pos) == '.' || sb.charAt(pos) == '?' || sb.charAt(pos) == '!') {
capitalize = true;
} else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) {
sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos)));
capitalize = false;
}
pos++;
}
FileWriter writer = new FileWriter(outputFile);
writer.write(sb.toString());
writer.close();
br.close();
System.out.println("-------------Output File Content---------------------");
System.out.println(sb.toString());
}
else{
System.out.println("Input file does not exist");
}
}
}
Output:
Please enter the input file
D:\testing.txt
Please enter output file
D:\testing1.txt
-----------Input file content--------------
Once upon! a time, there was a very bad man.
He smelled! funky, sang awful pop songs, and told annoying knock-knock jokes.
Nobody liked! him. then, one day, he slipped on some ice and died. Everyone was happy after that.
The end.
-------------Output File Content---------------------
Once upon! A time, there was a very bad man. He smelled! Funky, sang awful pop songs, and told annoying knock-knock jokes. Nobody liked! Him. Then, one day, he slipped on some ice and died. Everyone was happy after that. The end.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.