Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Require Assistance with StringBuilder/Classes/ and importing a file

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Require Assistance with StringBuilder/Classes/ and importing a file

    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    It is good to post your assignment when you have a question so we can fully understand what should be done.
    We also need you to ask a question and explain where you are stuck. Show any code you have so far.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Quote Originally Posted by jps View Post
    It is good to post your assignment when you have a question so we can fully understand what should be done.
    We also need you to ask a question and explain where you are stuck. Show any code you have so far.
    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.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Quote Originally Posted by jps View Post
    It is good to post your assignment when you have a question so we can fully understand what should be done.
    We also need you to ask a question and explain where you are stuck. Show any code you have so far.
    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 so far.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    What steps in the assignment have you done so far?
    What is the next step you need to do?

    When posting code, Please wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    This is all I have so far, I don't know where to go from there. I have to import a file and using StringBuilder I have to format it and Thank you I will do that.
    The exact description of the assignment "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. 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."

    import java.util.Scanner;
     
    public class FORMAT
     
    {
     
    public static void main(String[] args)
     
    {
     
    // Declarations
     
    Scanner input = new Scanner(System.in);
     
    }
     
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Can you ask a simple question about the next step you are working on instead of posting a full copy of the assignment?

    Is this the same problem: http://forums.devshed.com/java-help-...he-943080.html
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Yes, so should I delete this? How would I call in the .txt file so it can read from and then format it.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Use the Scanner class's constructor to connect to the file and use its methods to read the file.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Quote Originally Posted by Norm View Post
    Use the Scanner class's constructor to connect to the file and use its methods to read the file.
    I don't understand it

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Require Assistance with StringBuilder/Classes/ and importing a file

    Read the API doc for the Scanner class. There is an example there.
    Also do a search here on the forum for samples of the use of the Scanner class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 15th, 2013, 10:28 AM
  2. Require a variable in an interface?
    By sci4me in forum Object Oriented Programming
    Replies: 2
    Last Post: February 28th, 2013, 05:05 AM
  3. StringBuilder
    By Anoop Shiralige in forum Java Theory & Questions
    Replies: 1
    Last Post: June 23rd, 2012, 07:08 AM
  4. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 09:42 PM
  5. Replies: 5
    Last Post: December 13th, 2009, 07:10 PM

Tags for this Thread