Prompt for the maximum formatted output width the range is


Write a simple text formatting program in Java, that will read words from an input file and write them to an output file, removing extra whitespace and filling each line with as many words as will fit up to a maximum column width (similar to a column of text in a newspaper). The maximum column width, input text file name, and output text file name are entered interactively when the program starts.

The program needs to perform the following activities:

* Prompt for the maximum formatted output width. The range is 30 to 100 characters.

* Prompt for the file name of the input text file. Test that the file exists, and can be read. If so, then only open the file for reading. Repeat these steps until a valid input file is entered.

* Prompt for the file name of the output file to write the formatted words to. Test that the file does not already exist, and if it does, prompt to check for overwriting the file. Repeat these steps until a valid output file is entered.

* Write a line of * characters, the width of the formatted output, as a heading in the output file (no formatted output line should be longer than this heading line), for example, if 45 was entered as the maximum output width, this line should have 45 * characters.

* Read words (text between whitespace) from the input file, appending them to a line of text being formatted, with one space character between words. Keep appending words until the current word would cause the line to become longer than the output column width. If so, write the line to the output file, reset it to contain just the new word, and continue reading. Continue reading until the end-of-file is encountered.

* Do not read the entire file into memory, and do not use an array to hold multiple lines in memory.

Attached are sample input-output, and two sets of test data that should be run with a width of 40 and width of 70.

Attachment:- Sample output.zip

Solution Preview :

Prepared by a verified Expert
Operating System: Prompt for the maximum formatted output width the range is
Reference No:- TGS01253928

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)