logo CodeStepByStep logo

printBox

Language/Type: Java file input
Related Links:

Write a method named printBox that accepts two parameters: a string holding a file name, and an integer for a width. Your method should open that file and reads its contents as a sequence of lines, and display the lines to the console with a 'box' border of # characters around them on all four sides. The second parameter to your method indicates the total width of the box including its border. You must also convert each line to "title case" by capitalizing the first letter of the line and lowercasing all subsequent letters. For example, suppose the file poem.txt contains the following text:

roses ARE red
VIOLETS Are blUE

All my BASE
ARE belong To YOU

Then the following calls would produce the following console output. If any lines in the file are too long to fit into the box, truncate them.

printBox("poem.txt", 19); printBox("poem.txt", 30); printBox("poem.txt", 7);
###################
#Roses are red    #
#Violets are blue #
#                 #
#All my base      #
#Are belong to you#
###################
##############################
#Roses are red               #
#Violets are blue            #
#                            #
#All my base                 #
#Are belong to you           #
##############################
#######
#Roses#
#Viole#
#     #
#All m#
#Are b#
#######

You may assume that the input file exists and is readable, and that its lines can fit within the given width. You may assume that the width value passed will be at least 2. Notice that the file might contain blank lines. Your solution should read the file only once, not make multiple passes over the file data.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.