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 16 of 16

Thread: Formatting a text file

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Formatting a text file

    I'm trying to follow directions to format a text file but I'm very new at Java and I'm not exactly sure how to do this formatting. Here are the instructions:


    "read in a text file and display the contents of the file to the screen output. The file will contain words separated by spaces, lines terminated by the newline character(s), and paragraphs separated by a blank line (i.e., a line with zero (0) characters in it).
    Your task is to 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."

    This is all the code I have and I'm extremely stuck. If anyone can help me get started with this and help me understand I'd really appreciate it.



    package format;
     
    import java.util.Scanner;
    import java.io.*; 
     
     
    public class Format 
    {
     
     
    public static void main(String[] args)
    throws IOException 
    {
     
     
    Scanner input = new Scanner(System.in);
    FileWriter fwriter = new FileWriter("Text.txt", true);
    PrintWriter inputFile = new PrintWriter(fwriter); 
     
     
     
    inputFile.close();



    }






    }


  2. #2
    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: Formatting a text file

    Can you explain what problems you are having and ask some specific questions?

    Please edit your post and 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.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    I'm unsure on how to use Stringbuilder to do this. I was hoping someone would be able to show me how to continue with this program.

  4. #4
    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: Formatting a text file

    Can you explain your problem using the StringBuilder class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    Quote Originally Posted by Norm View Post
    Can you explain your problem using the StringBuilder class.
    I don't know how to use it, that's my problem. I have absolutely no idea how to format a file with a block of text. Can you give me help with that?

  6. #6
    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: Formatting a text file

    how to format a file
    Use a class like the PrintWriter class to write text to the file. To see how it works, write a small, simple program that creates a PrintWriter object to write to a file and call some of its methods, close the writer and look at the file that was written to see what happen. Do some experimenting with different methods and see what is written.

    I'm not sure what you mean by "format a file" and "block of text".
    Bytes of data are written to files.
    text in java programs is held in Strings.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Formatting a text file

    I was given a text file that I must do what is in the instructions I posted. I don't know how to read in each line(there's a lot of them). I would like to read in the lines and set the lengths.

  8. #8
    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: Formatting a text file

    Did you work out how to use the PrintWriter class? I'd recommend working on one thing at a time, getting it to work and then move on to the next thing. Trying to do it all at one time usually complicates things.

    how to read in each line
    The Scanner class could be used for reading text files. See the API doc for the class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    Yes, I've figured out the Printwriter class. Is there a way to take in a bunch of lines and specifying their lengths? My earlier question was how do I use StringBuilder to do this?

  10. #10
    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: Formatting a text file

    Is there a way to take in a bunch of lines and specifying their lengths?
    If the lines are in String objects, the String class has a method that gives the length of the String.

    The StringBuilder class is used to build Strings. Use its methods to add data to it. When you want its contents in a String call another method that returns the contents as a String.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    Did you read what my instructions are and do you get what they're asking? Could you possibly give me some sample code to just demonstrate formatting the lines?

  12. #12
    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: Formatting a text file

    Start by experimenting with the StringBuilder class to see how it works and what can be done with it.

    Perhaps when you see how to use the class's methods, you'll get some ideas about the assignment.

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    I've tried using StringBuilder but I don't know how to make it read the lines of my file. Can you please explain this a little bit clearer? I hardly have any experience so just telling me to experiment isn't helping much

  14. #14
    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: Formatting a text file

    using StringBuilder but I don't know how to make it read the lines of my file
    StringBuilder does not read files. Use the Scanner class to read files.

    The assignment should specify what to put into the StringBuilder.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting a text file

    The file must be inputted into the program. Stringbuilder is supposed to format each line. I have no idea how to do this. I don't know how to make the connection between scanner, my inputted text file, and stringbuilder. That's all I'm asking.

  16. #16
    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: Formatting a text file

    The StringBuilder class is a place to put different types of data that you want to see in a String. I'm not sure what you mean by "format each line".
    the connection between scanner, my inputted text file, and stringbuilder.
    The Scanner methods will read the lines from the text file into a String.
    The StringBuilder class can take that String and use it to build a larger String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  2. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  3. [SOLVED] Printwriter class: displaying File contents to screen with included formatting?
    By mwebb in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: November 25th, 2011, 11:38 AM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM