Write a program that checks a text file for several formatting and punctuation matters. The program asks for the names of both an input file and an 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 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 whitespace characters.

This project is deceptively simple. The problem statement is clear enough, with only a couple ambiguities to clarify. The solution shown here assumes that there is at least one line in the file to process and keeps all tabs and newlines unless they precede the first word on the first line. A helper method processes the first part of the first line to remove all leading white space and capitalize the first letter. After that, main simply reads one line of text at a time until it gets a null string, and uses another helper method to process the remaining text. This helper method is where most of the work is done: It processes each line, character by character, and uses flags to control the processing; to print only one space when there more than one in sequence, and to capitalize the first word in each sentence. Notice how the helper method to convert a character to upper case is written. The code first checks to see if the character is a lower case letter, and, if it is, it does integer arithmetic to convert the ASCII lower case code to the ASCII upper case code. A check of an ASCII chart will show that the upper case codes are 32 less than those for lower case. After doing the subtraction it is necessary to cast the integer result back to char to match the method’s return type, char.


See the code in WriteSentenceTextFile.java, DisplaySentenceTextFile.java, and EditSentenceTextFile.java.

Computer Science & Information Technology

You might also like to view...

Data recovery ____ provide a hardware and software environment that is compatible with the conditions of the primary site, as well as the most recent backup of the data.

A. coldsites B. warmsites C. hotsites D. procedures

Computer Science & Information Technology

The value of a given number is found by __________.

a. multiplying each digit by its place or positional value b. adding the products c. neither A nor B d. both A and B

Computer Science & Information Technology

Which DirectoryChooser method returns the folder the user selected?

a. getSelectedfolder. b. showDialog. c. getOpenDialog. d. showOpenDialog.

Computer Science & Information Technology

The tag may or may not be used within the element

Indicate whether the statement is true or false

Computer Science & Information Technology