Home > > WordWrap


Hello. I made this code for a school assignment. I need some help with it. Its a method called wordWrap that accepts a Scanner representing an input file as it parameter and outputs each of the file to the console, word wrapping all lines that are longer than 60 characters. So if a line contains 112 characters it prints two lines to the console, the first with 60 characters and the second with 52 characters. If 217 characters, 4 lines of 60 and the final line lenght 37 etc. So far so good, the code does this. But I want the method to work so it also outputs the newly wrapped text back into the original file. It will also do this. But only when i run it the first time. The second time and so on, it only outputs the last line back to the original file.
Can anyone explain me why it only outputs the last line back to the original file, and maybe how to change the code so it will output the hole original text back into the original file.
/*the code
doesn't work when inputFile is formatted the same as console output
then it only prints out a couple of words to the inputFile
needs a input file named inputFile.txt with some text in it. The file should be in the same dictionary as the .java file
import java.io.*;
import java.util.*;
public class Ch6ex3
public static final int lines = 60;
public static void main(String[] args) throws FileNotFoundException
public static void wordWrap(String inputFile) throws FileNotFoundException
String word="";
String word2="";
String remainWords ="";
String remainWords2 ="";
File f = new File(inputFile);
Scanner input = new Scanner(f);
while (input.hasNextLine())
word = input.nextLine();
word2 = word;
while (word.length() > lines)
String first60 = word.substring(0, lines);
word = word.substring(lines);
//variable that saves the remaining
remainWords = remainWords + word;
//prints the remaining when word is less than 60 char.
//outputs the newly wrapped text back into the original file
          PrintStream out = new PrintStream(f);
               while (word2.length() > lines)
     String second60 = word2.substring(0, lines);
     word2 = word2.substring(lines);
     //variable that saves the remaining
     remainWords2 = remainWords2 + word2;
baudits, Ignore the "rude" tone of this post... it's not directed at you personaly, but it does contain useful information, especially bone up on {code} tags.
h1. The Ubiquitous Newbie Tips
* Homework dumps will be flamed mercilessly.
* Have a quick scan through the [Forum FAQ's|http://wikis.sun.com/display/SunForums/Forums.sun.com+FAQ].
h5. Ask a good question
* Don't forget to actually ask a question. No, The subject line doesn't count.
* Ask once
    - Don't Crosspost!
    - Two people answering one question independantly is a waste of there time.
* Don't even talk to me until you've:
    (a) [googled it|http://www.google.com.au/] and
    (b) looked it up in [Sun's Java Tutorials|http://java.sun.com/docs/books/tutorial/] and
    (c) read the relevant section of the [API Docs|http://java.sun.com/javase/6/docs/api/index-files/index-1.html] and maybe even
    (d) referred to the JLS (for "advanced" questions).
* [Good questions|http://www.catb.org/~esr/faqs/smart-questions.html#intro] get better Answers. It's a fact.
    - Many of the regular forumites don't bother to read badly written questions.
      - FFS spare us the SMS and L33t speak! Pull your pants up! and Get a hair cut!
    - Often you discover your own mistake whilst formulating a "Good question".
    - Many forumites will bend over backwards to help with a "Good question",
      especially to a nuggetty problem, because they're interested in the answer.
* Improve your chances of getting laid tonight by writing an SSCCE
    - For you normal people, That's a: Short Self-Contained Compilable Example.
    - Short is sweet: No-one wants to wade through 5000 lines to find your syntax errors!
    - Often you discover your own mistake whilst writing an SSCCE.
    - Solving your own problem yields a sense of accomplishment ;-)
h5. Formatting Matters
* Post your code between a pair of {code} tags
    - That is: {code} ... your code goes here ... {code}
    - this makes code easier to read by preserving whitespace and syntax highlighting.
    - Copy&paste your source code directly from your editor. The forum editor sucks.
    - The forums tabwidth is 8 (as per the convention).
      - Indentation will go wacky if you've mixed tabs and spaces.
      - Proper indentation illustrates program logic.
      - Lines longer than 80 (or so) characters should be wrapped.
* Post your error messages between a pair of {code} tags:
    - That is: {code} ... errors here ... {code}
    - Please mark the erroneous line(s) in your source-code. For example:
        System.out.println("Your momma!); // <<<< ERROR 1
    - Error messages may be rendered useless if the code has been
      modified since the error message was produced.
    - Here's [How to read a stacktrace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/].
&#42; The forum editor has a "Preview" pane. Use it.
    - If you're new around here you'll probably find the "Rich Text" view is easier to use.
    - WARNING: Swapping from "Plain Text" view to "Rich Text" scrambles the markup!
    - To see how a posted "special effect" is done, click reply then click the quote button.
If you (the noob) have covered these bases *you deserve, and can therefore expect, GOOD answers!*
h1. The pledge!
We the New To Java regulars do hereby pledge to refrain from flaming anybody, no matter how gumbyish the question, if the OP has demonstrably tried to cover these bases. The rest are fair game.
Read the other 4 answers