gkaindia.com

Home > > WordWrap

WordWrap

Advertisement
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.
Thanks.
/*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
wordWrap("inputFile.txt");
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);
System.out.println(first60);
word = word.substring(lines);
//variable that saves the remaining
remainWords = remainWords + word;
//prints the remaining when word is less than 60 char.
System.out.println(word);
//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);
     out.println(second60);
     word2 = word2.substring(lines);
     //variable that saves the remaining
     remainWords2 = remainWords2 + word2;
               out.print(word2);
}
Advertisement
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
* DON'T SHOUT!!!
* 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
Sorry if the question seems dumb, but is there a way to insert a space in a block of text that won't break up a word during wordwrapping? Example : « This is a test » I like the spaces between the quotation marks and the quoted text. But I don't want
I am new to acrobat.  I converted a word document to a pdf, started the forms wizard and detected the text fields, but how do I set a field to wordwrap without the text getting smaller and smaller????????????????I have Adobe Acrobat Pro X. I cannot f
Using a single column and row table to post notes from a data run. The notes may be long and when using a constant font size the string goes off the page, hense the need for WordWrapping Attachments: Jims_Try.TDR ‏59 KB tdmtest.tdm ‏15 KBHello Todd,
In code view window, with wordwrap, dreamweaver hides code characters on the right edge of window. It only hides 3-4 last character of the largest line. Is it a bug? Does anyone notice it?I have also faced this issue where if code's line is large and
I'm using mx:List to display String items. The dataProvider is XMLListCollection. Some of the String is long so it takes more than one rows. I use the wordWrap to display the long String in more than one rows. The problem is that the next String item
Hi,    Does anyone have an idea how to get wordwrap and  variablerowheight work for OLAPDataGrid, Here is the example which I am  trying but it is not working. <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx=&q
I would like to establish a line length for email that I compose or receive. How can I do that? How can I get TB to wordwrap at that length and not some shorter length?I did that - and found several items for wrap. All except the one noted - mailnews
Anyone know how we can do wordwrap of nodes in jtree? Please help... Thanks.Any example on this? what html code should I use? The simplest possible, eg yourNode.setText("<html>" + yourNodeText + "</html>");Most JComponents
Anyone know how to get a flash quiz template checkbox to set to multi line? I have tried: onClipEvent (load) { this.checkbox.labelPath.multiline = true; this.checkbox.labelPath.wordWrap = true; and also on the keyframe that contains the checkbox text
I have been attempting to get my data grid to properly word wrap. The problem seem to be that the height of the row with the is set for all rows by the first occurrence and not row by row. Is this correct? Is it possible to make each row size to the
I use the deveooper's edition of FF (like the debugger FYI) but i've got a problem now. since the upgrade today, text areas in some of the forums I visit no longer have wordwrap in them. I checked the settings and can't find them. I also searched the
Hi, I coded it this way: <mx:DataGridColumn wordWrap="true" width="200" headerText="Comments" dataField="comments"> The text value doesn't wrap around though. It gets clipped. What am i missing? Thanks in adva
Is there a way to make a panel's title be multiline? I've got a panel where the title is dynamic. I don't know how long the title will be. As it works now, the the title occaisionally gets cut-off. Is there a way to wordwrap a panel's title? <mx:Pane
This is a major source of annoyance for me, but may well be due to my own lack of understanding: When i receive an email on my PB though the bridge with my Torch, for the first second of viewing the text wordwraps perfectly, but then before my very e
Hello When i fill out the detailed description of KM folders, the text string appears in its whole length. I know that its possible to set the modifier "maxChars" in resource renderer settings to trim the textstring to a defined length. steps to
Im trying to draw two string on two different possotions. But only the last String will be drawn (out[1])... Why is that? The code i based on this wordwraper http://cvs.sourceforge.net/viewcvs.py/kobjects/utils4me/src/org/kobjects/lcdui/WordWrap.java
Here's my form's code   <cfform name="displayProjects" method="post" preservedata="yes" format="flash" action="" id="myformid"  width="800" height="1000" wmode="transp
Hi, how can I realize a wordwrap in the text of FM popup_to_inform?         CALL FUNCTION 'POPUP_TO_INFORM'           EXPORTING             titel = 'Benutzer-Information:'             txt2  = 'Ihre Anfrage wurde per Mail an den zuständigen Entwickler
I need help with word wrapping a SelectOne type object in a table cell The text of some of the choices is long and needs 1 wrap/berak SelectOneListBox does not warp and the cell of the table becomes very wide to accompdate the few long list texts Sel