Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default Creating a word wrap

    so im stuck on a problem and I need some help. I have some code written but I am unsure about how to go further. Here is the problem:

    Write a program called WordWrap.java. This program has a wrapWord method that accepts
    a Scanner parameter reading input from a file (the filename is either longline.txt or a name
    accepted from the console), and outputs each line of the file to the console, word-wrapping
    all lines that are longer than 60 characters. For example, if a line contains 112 characters,
    the method should replace it with two lines: one contains the first 60 characters and another
    contains the final 52 characters. A line containing 217 characters should be wrapped into
    four lines: three of length 60 and a final line of length 37. If a word needs to be chopped in
    half at the boundary of the 60th and 61th characters, a hyphen should be added at the end of
    the first line.

    Example:
    This is a very long line that should be wrapped into the following two lines.

    Output
    This is a very long line that should be wrapped into the following
    two lines.


    here is my code so far:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class WordWrap {
    
        public static void main(String args[]) throws FileNotFoundException {
        Scanner input = new Scanner(new File("longline.txt"));
            while (input.hasNextLine()) {
              String line = input.nextLine();
             wrapWord(line);
            }
        }
        
        public static void wrapWord (String line) {
        int length = line.length();
            while (length >= 60){
            
            }
                
            
    
        }
    }
    My problem is I dont know what to put in the while loop. I know i am supposed to find the length of the string but how do put the hyphen in there for any string with any amount of characters. I am basically stuck trying to figure out how to get the output. It read the text file fine, but I can't seem to get anything to work. Any help would be appreciated.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    First, I think you set up your method wrong as directed in the instructions. It says to pass in a Scanner as a parameter like this:

    Java Code:
    public static void wrapWord (Scanner input) 
    {
    }

    This would change your main method to look more like:

    Java Code:
    public static void main(String args[]) throws FileNotFoundException 
    {
         Scanner input = new Scanner(new File("longline.txt"));
         wrapWord(input);
    }

    As a hint to your while loop. I would use modulus. I've given you the code to setup your program below, I think this will give you a better head start than where you were at before. If you have any questions as to why I did certain things, or if you don't understand, please let me explain.

    Java Code:
    public static void wrapWord (Scanner input) 
    {
         String temp = "";
        	
         while(input.hasNextLine())
        	 temp += input.nextLine(); // Creates one big string to run calculations on (hint: modulus).
        	
         int length = temp.length();
        	
         for(int i = 0; i < length; i++)
         {
              // Calculations on string go here.
         }
    }
    Last edited by joshdgreen; 10-29-2010 at 04:45 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I wonder if it such a good idea to join all the input together.

    As I understand it

    Java Code:
    This is a very long line that should be wrapped into the following two lines.
    This is a very long line that should be wrapped into the following two lines.

    should become

    Java Code:
    This is a very long line that should be wrapped into the fol-
    lowing two lines.
    This is a very long line that should be wrapped into the fol-
    lowing two lines.

    But if you join them you get (sorry for the long line - but note the full stop)

    Java Code:
    This is a very long line that should be wrapped into the following two lines.This is a very long line that should be wrapped into the following two lines.

    which then would be split as

    Java Code:
    This is a very long line that should be wrapped into the fol-
    lowing two lines.This is a very long line that should be wr-
    apped into the following two lines.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Yeah, but he's only asked to evaluate one continuous line as far as I know, or one continuous sentence or paragraph without new paragraphs.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    OK. I interpreted that wordWrap() "outputs each line of the file to the console, word-wrapping all lines that are longer than 60 characters" as meaning multiple lines had to be processed, and processed as distinct lines. Perhaps the original poster could clarify

  6. #6
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default Questions

    So I'm pretty new to all of this, so I am not sure on how to go about calculating the string. Also where are you hint I use the modulus?

  7. #7
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    Perhaps the original poster could clarify

    Yeah that would be nice. Reading what you just posted, I'm now unsure myself.
    Sincerely, Joshua Green
    Please REP if I help :)

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by huntrguy102 View Post
    So I'm pretty new to all of this, so I am not sure on how to go about calculating the string. Also where are you hint I use the modulus?

    You will use the modulus inside the "for loop" FOR calculating the string.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    to answer you question. I just have to take the one lined sentence:

    "This is a very long line that should be wrapped into the following two lines."

    and the output to the console should be:

    "This is a very long line that should be wrapped into the fol-
    lowing two lines."

  10. #10
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by huntrguy102 View Post
    to answer you question. I just have to take the one lined sentence:

    "This is a very long line that should be wrapped into the following two lines."

    and the output to the console should be:

    "This is a very long line that should be wrapped into the fol-
    lowing two lines."

    Thanks for clarifying :D
    Sincerely, Joshua Green
    Please REP if I help :)

  11. #11
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    I'm still confused about how using the the modulus function to calculate the length and split it up into different lines would work. Maybe explain more of what your sample code above means. Again I'm a beginner, sorry for my ignorance.

  12. #12
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Everytime you go through the loop you are increasing a counter "i" by 1 right? What the modulus does is calculate a remainder. The Remainder or Modulus Operator in Java

    The key number you will be looking for is line 60. So when i = 60, you need a new line. Using that modulus operator we can create a statement:

    Java Code:
    if( (i % 60) == 0 )

    Which will tell us when to insert our line break. Notice that even if we are on line 120, the statement will still be true: 120 / 60 has no remainders.
    Sincerely, Joshua Green
    Please REP if I help :)

  13. #13
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    ok so if I have something with 72 characters it will divide by 60 and get a remainder other than 0. So how do I go about telling it at character 60 to create a new line and add a hyphen?

  14. #14
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    ok so I put something like this in the for loop

    Java Code:
    for(int i = 0; i < length; i++)
    {
         if( (i % 60) == 0 ) {
         String wordBreak = temp.substring(i);
         System.out.println("-" + wordBreak);
    
         }
    }
    and it gave me an output like this:

    "-This is a very lone line that should be wrapped into the following two lines.
    -lowing two lines."

    what am I doing wrong?

  15. #15
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    The "if" statement I posted above tells the code to DO SOMETHING every 60 lines. Just put your new line statement there. You'll have to add a few more things to the statement to get it to calculate the hyphen. Here's a hint:

    Java Code:
    if(temp.charAt(i - 1) == ' ')

    This will check to see if the character at position 59 is a blank space. You need to check all the characters around character 60 to see if they are blank spaces because then you will NOT need a hyphen. It's only when there are characters AT positions 59, 60, and 61 that you will need to insert a hyphen.
    Sincerely, Joshua Green
    Please REP if I help :)

  16. #16
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by huntrguy102 View Post
    ok so I put something like this in the for loop

    Java Code:
    for(int i = 0; i < length; i++)
    {
         if( (i % 60) == 0 ) {
         String wordBreak = temp.substring(i);
         System.out.println("-" + wordBreak);
    
         }
    }
    and it gave me an output like this:

    "-This is a very lone line that should be wrapped into the following two lines.
    -lowing two lines."

    what am I doing wrong?

    Try replacing the code inside the "if" statement with just this line and see what happens. Experiment with that and the info I posted above.

    Java Code:
    System.out.println();
    Sincerely, Joshua Green
    Please REP if I help :)

  17. #17
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    so here is an update on what i did

    Java Code:
    if( (i % 60) == 0 ){
    		String wordBreak = temp.substring(0,i);
    		System.out.println(wordBreak);
    		
    	
    		}
    an here is the output:

    Java Code:
    This is a very long line that should be wrapped into the fol
    so im doing something right. but how do I get the other part of the sentence to show up?

  18. #18
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Get rid of the wordBreak variable and just print out a blank line like stated above ^ ^
    Sincerely, Joshua Green
    Please REP if I help :)

  19. #19
    huntrguy102 is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    so more like this?

    if( (i % 60) == 0 ){
    System.out.println();
    }

  20. #20
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Yes, that looks like it will work.
    Sincerely, Joshua Green
    Please REP if I help :)

Page 1 of 2 12 LastLast

Similar Threads

  1. I'm having a problem trying wrap up this code
    By feastfulsaint in forum Advanced Java
    Replies: 12
    Last Post: 07-17-2010, 08:27 PM
  2. creating word writer
    By Anchal in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2010, 10:00 AM
  3. JTextArea Wrap Around or something?
    By Krooger in forum New To Java
    Replies: 6
    Last Post: 11-28-2009, 05:29 AM
  4. Word Wrap not functioning correctly?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-08-2009, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •