So I have public static Format setup
my project is to import a file and format it using the stringBuilder
Write the program Format which will take a single command line argument to
specify the width of the output line. The name of the file to be formatted will be entered as
redirected standard input. The output will be displayed to the standard output (i.e., the screen).
To read from a disk file you will need to import the classes
java.util.Scanner
To create a
Scanner
class object instance for the redirected standard input (the file to be
formatted) include the following lines:
Scanner input = new Scanner(System.in);
Your program will read from the scanner
input
a line at a time. For each line read, if the line
has a zero (0) length then just display a blank line. Otherwise, the line will contain words.
Create a
new
Scanner
object instance whose constructor takes the name of the
String
that
contains the line you just read in. You can then pick up each word using the
next()
method.
You will need a
StringBuilder
object instance to hold the output line that you are building up.
You then append words to the end of the output
StringBuilder
. Remember to add a single
blank space between each word (but not before the first word in the output
StringBuilder
).
Before adding a word you should check that the current length of the string plus the length of
the new word and the blank space does not exceed the output line width that the user specified
at the beginning of the execution of the program. If the new word will cause the output string to
become too long then the unaugmented line will be displayed, the output line
StringBuilder
should be cleared (reset to have 0 length) and the new word added to the empty string. Be
aware that when the file is completely read there might be some words left in the output
StringBuilder
. These should also be displayed to the screen. Close all scanners when they
have completed their tasks.
Page
1
of
2
CST141
Principles of Computing
Format
You should include some statistics gathering and display to your program. Keep track of the
number of lines and words read in as well as the number of lines and words displayed.
I am including two text files with this project. The first is the text of the Declaration of
Independence (
DoI.txt
). This file will serve as the input file to the formatting program. The
second is the result of running the program (
DoI.fmt
). You should use these files to guide you
in developing the program.
Extra Credit extension:
Text formatters frequently have a feature that allows the program to be justified against both the
left and the right margins. In character based formatters this is handled by adding additional
blank space characters to the word separating spaces already being used in the output lines.
You will need to figure out where the extra blanks are inserted into the output line. If the number
of blanks needed is less than the total number of spaces in the output line then add one (1)
extra blank to each space starting on the right and progressing toward the left. If the number of
blanks needed is greater than the total number of spaces in the output line then add one (or
more, as needed) blanks to
each
space and any extra blanks are added, as above, starting on
the right and working left. The sample output file shows what the justified output should look
like.