Prompt for the file name of the output file to write the formatted words to. Use a File object to test that the file doesn't already exist, and if it does, prompt to check for overwriting the file. Open the file for writing using a PrintWriter. Use a do-while loop to repeat these steps until a valid output file is entered.
Loop, reading words (text between whitespace) from the input file using Scanner.next(), appending them to a line of text being formatted, with one space character between words. Keep appending words until the current word would cause the line to become longer than the output column with. If so, write the line to the output file, reset it to contain just the new word, and continue reading. Continue reading until the end-of-file is encountered, using Scanner.hasNext().
I was trying to code it based on another tutorial but that one writes a line to a file hardcoded in the program. The biggest issue I'm having is the second instruction. Here is what I put together so far:
System.out.println("Enter output file name or 'console'.");
String outName = sc.nextLine();
PrintWriter outWriter;
try{
outWriter = new PrintWriter(new FileWriter(outName));
outWriter.write("");
outWriter.close();
}catch (IOException e){
System.out.println(e.toString());
}

New Topic/Question
Reply


MultiQuote





|