#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Help with String Builder/Classes and how to import the file


    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    So, what have you tried so far. This isn't a free 'come here, post & some one will do your homework for you' forum. You have to put the effort in first. At least make an attempt.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by doa24uk
    So, what have you tried so far. This isn't a free 'come here, post & some one will do your homework for you' forum. You have to put the effort in first. At least make an attempt.
    I have setup the FORMAT class and I will post what I have so far tomorrow morning but its embarrassing because I have no idea on how to go about it. Thank you for you reply.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    Originally Posted by bradwynn
    I have setup the FORMAT class and I will post what I have so far tomorrow morning but its embarrassing because I have no idea on how to go about it. Thank you for you reply.
    No problem, don't be embarrased. No-one here was born with Java knowledge . Hopefully, we're all learning too as I know I certainly don't know everything!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Talking


    Originally Posted by doa24uk
    No problem, don't be embarrased. No-one here was born with Java knowledge . Hopefully, we're all learning too as I know I certainly don't know everything!
    import java.util.Scanner;

    public class FORMAT

    {

    public static void main(String[] args)

    {

    // Declarations

    Scanner in = new Scanner(System.in);

    }

    }

    This is all I know and it is due today 11:59 PM EST.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    FYI, it is convention to name your classes with the first letter capitalised, not all of it... eg. Format not FORMAT.

    Here is the bare bones of a working line by line scanner input. Have a look, understand it properly... move the System.out.print statements around & see what effect it has on the output.

    I've added the differences in the print statements (the bracketed portions) so you can see which is getting called & when.

    I've also tried to comment it as plainly as I can so that should help.

    Code:
    public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            
            System.out.print("Please input something (this is the first time I display): ");
            while (in.hasNextLine()) // This reads an entire line of text. Hit enter on your keyboard to progress.
            {
                String enteredLine = in.nextLine(); // Places the line into the 'enteredLine' String variable
                if (enteredLine.isEmpty())
                {
                    System.out.println("The line you entered was empty. Exiting the loop.");
                    break; // This exits whichever loop is the first outer one. In this case, the while.
                }
                else // We have some text to show.
                {
                    System.out.println("You entered: " + enteredLine);
                }
                System.out.print("Please input something (this is inside the while loop): ");
            }
            // When break is called, we end up here... Nothing to do so we exit the main method.
        }
    Last edited by doa24uk; April 7th, 2013 at 10:58 AM.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by doa24uk
    FYI, it is convention to name your classes with the first letter capitalised, not all of it... eg. Format not FORMAT.

    Here is the bare bones of a working line by line scanner input. Have a look, understand it properly... move the System.out.print statements around & see what effect it has on the output.

    I've added the differences in the print statements (the bracketed portions) so you can see which is getting called & when.

    I've also tried to comment it as plainly as I can so that should help.

    Code:
    public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            
            System.out.print("Please input something (this is the first time I display): ");
            while (in.hasNextLine()) // This reads an entire line of text. Hit enter on your keyboard to progress.
            {
                String enteredLine = in.nextLine(); // Places the line into the 'enteredLine' String variable
                if (enteredLine.isEmpty())
                {
                    System.out.println("The line you entered was empty. Exiting the loop.");
                    break; // This exits whichever loop is the first outer one. In this case, the while.
                }
                else // We have some text to show.
                {
                    System.out.println("You entered: " + enteredLine);
                }
                System.out.print("Please input something (this is inside the while loop): ");
            }
            // When break is called, we end up here... Nothing to do so we exit the main method.
        }
    How do I call in the file that it is suppose to read in from
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    Read this.

IMN logo majestic logo threadwatch logo seochat tools logo