Results 1 to 20 of 26
Thread: Creating a word wrap
- 10-29-2010, 01:33 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
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:
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.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){ } } }
- 10-29-2010, 03:43 AM #2
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 03:45 AM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 05:50 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 10-29-2010, 06:01 AM #4
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 :)
- 10-29-2010, 06:05 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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
- 10-29-2010, 06:18 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
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?
- 10-29-2010, 06:20 AM #7
- 10-29-2010, 06:22 AM #8
- 10-29-2010, 06:23 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
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-29-2010, 06:24 AM #10
- 10-29-2010, 06:27 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
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.
- 10-29-2010, 06:31 AM #12
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 :)
- 10-29-2010, 06:39 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
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?
- 10-29-2010, 06:47 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
ok so I put something like this in the for loop
and it gave me an output like this:Java Code:for(int i = 0; i < length; i++) { if( (i % 60) == 0 ) { String wordBreak = temp.substring(i); System.out.println("-" + wordBreak); } }
"-This is a very lone line that should be wrapped into the following two lines.
-lowing two lines."
what am I doing wrong?
- 10-29-2010, 06:50 AM #15
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 :)
- 10-29-2010, 06:51 AM #16
- 10-29-2010, 06:56 AM #17
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
so here is an update on what i did
an here is the output:Java Code:if( (i % 60) == 0 ){ String wordBreak = temp.substring(0,i); System.out.println(wordBreak); }
so im doing something right. but how do I get the other part of the sentence to show up?Java Code:This is a very long line that should be wrapped into the fol
- 10-29-2010, 07:07 AM #18
Get rid of the wordBreak variable and just print out a blank line like stated above ^ ^
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 07:09 AM #19
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
so more like this?
if( (i % 60) == 0 ){
System.out.println();
}
- 10-29-2010, 07:10 AM #20
Similar Threads
-
I'm having a problem trying wrap up this code
By feastfulsaint in forum Advanced JavaReplies: 12Last Post: 07-17-2010, 07:27 PM -
creating word writer
By Anchal in forum AWT / SwingReplies: 3Last Post: 04-06-2010, 09:00 AM -
JTextArea Wrap Around or something?
By Krooger in forum New To JavaReplies: 6Last Post: 11-28-2009, 04:29 AM -
Word Wrap not functioning correctly?
By zerkz in forum New To JavaReplies: 2Last Post: 10-08-2009, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks