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();
}
}