The goal of this lab is to write a class MaxWidthPrinting that receives
from the user a positive integer called width and a file name and
prints the tokens from the file so that each line in the print out
has no more than width characters while minimizing the total number of
lines by maximizing the consective number of tokens appearing in each
line. Two rules apply. First, if more than one work appears in one
line, neighboring words should be separated by exactly one white space
character. Second, a word whose length is greater than or equal to
width should appear as a line containing just that word.
Suppose that the value of width is 15 and the first five lines of
the input text file are as follows:
But, in a larger sense, we can not dedicate --
we can not consecrate -- we can not hallow --
this ground. The brave men, living and dead,
who struggled here, have consecrated it,
far above our poor power to add or detract.
The first line “But, in a” has 9 characters.
Adding the next token “larger” to this would increase
the length to 9 + 1 + 6 = 16. Here the additive term of 1 is for
the required one whitespace character. So additing “larger”
would create an overflow, and so, we move on to a fresh new line,
with “larger” as the first word, leaving behind
“But, in a” as a printed line. After “larger”
the second line acquires the next token “sense,” thereby
turning the length to 13. Since the ensuing token “we”
has length 2 and there is an additional whitespace character between
the words, adding “we” would change the length to 16,
and so the word cannot be added. There are only three lines that
fully utilize the 15 character limit:
The code goas as follows. First, we will receive the width value
and the file name and create a Scanner object out of the file name.
To generate a print-out with maximum width we use a while loop
that takes action as long as the scanner has the next token. We
record the number of characters that have been used in the line
currently being generate using an int variable currentLength.
The initial value of this variable is 0. In the loop body, the
first thing we do it to read the next token from the file and store
it in a String variable word. By the end of the loop body, we
will have printed the word on the screen. Let wordLength be the
length of this token. There are five possible cases:
Case 1. currentLength == 0 and wordLength < width:
Print word and then set currentLength = wordLength
Case 2. currentLength == 0 and wordLength >= width:
Print a new line and then print word with a newline
Case 3. currentLength > 0 and wordLength >= width:
Print a new line, print word with a newline, and then
set currentLength = 0
Case 4. currentLength > 0 and wordLength < width and
the remaining capacity of the current line is insufficient
to accomodate word:
Print a new line, print word, and then
set currentLength = wordLength
Case 5. currentLength > 0 and wordLength < width and
the remaining capacity of the current line is sufficient
to accomodate word:
Print one white space, print word, and then
set currentLength = currentLength + 1 + wordLength
The sufficiency appearing in Case 4 can be checked by comparing
width with currentLength + 1 + wordLength.
After the conclusion of the while-loop, if the currentLength is
positive, then an additional new line character is needed.
Here is an another example, with a part of Moby Dick as an input: