hi guy,

i am trying to create a text wrapping method
soemthing like this

public void textwrap( String text, int width){

}

what i need it to do is to print the text in a column of specified width.
for example a user set width to 20, what the program needs to do is print words out in a column that is no bigger that 20 characters. words can not be cut they have to be full words so if the sum of two words ends up being more than 20 characters than just print out one word.


i been stuck on this for quite a while so any help will be apreciated
p.s the "text" variable is defined from a .txt file


thank you

Recommended Answers

All 15 Replies

The method is to print the String and pad it with spaces(?) or truncate it to width?

Where are the words you talk about? Is the method to parse the String and split it at the white spaces to produce words?

Can you give several examples of what you what the method to do?
Give it different String inputs and show what the print out should be.

You can use this pseudocode

1. Read word by word from the txt file
2. set a width counter variable (init = 0)
3. check if widthcounter+wordLength <=20, if so print it and add wordLength to widthcounter
4. else go to new line and start over....

Think of some way to incorporate blank spaces between words...

for example text variable contains this text

Triometric creates unique end user monitoring products for high-value Web
applications, and offers unrivalled expertise in performance consulting.

if column width is set to 20 it would result in the following output:

Triometric creates
unique end user
monitoring products
for high-value Web
applications, and
offers unrivalled
expertise in
performance
consulting.

if column width is set to 2;

Triometric
creates
unique
end
user
monitoring
products
for
high-value
Web
applications,
and
offers
unrivalled
expertise
in
performance
consulting.


thank you

Thats what use my pseudocode....

pseudocode is not correct there is one loop that checks weather there are any words left in the string and other one that should be adding them, i know the logic how to solve this problem, but i dont know how to actualy put it in use that is why i am posting this here

thanks

i know the logic how to solve this problem

If you have the logic, can you write some pseudo code describing what you want the code to do?

Are the words separated by spaces?
Find the last space before the width and split the String there.

well,
loop through the words lets say i would use string tokenizer,
check the lenght of the first word if it is less than specified width try adding one more word if the width now is greater than specified width then only print the first word and the second word would go on the new line now.
after first check try adding third word if lenght is greater than specified only print two words else add fourth word check if lenght is not bigger than specified if not bigger add fifth word else print three words and so on and on and on.
the idea is is not to exceed the width limit. also not split the single word apart and not just have max of two words in a line it could be hundreds of words in one line as long as their lenght does not exceed the specified width

hope this makes it clearer

thank you

A better way to get all the words in a String would be to use the split method.
That assumes that all the words are separated by spaces.
Then you have all the words in an array and can look at them one by one, building the output String as you go and testing if adding the next word from the array would put you past the the maximum allowed width.

thanks for your responce but i still can figure it out how can i make this dynamic, i have mentioned above how it the logic of the program should look like but it uses if statments which is not that dynamic, any solutions ?? step by step pseaduo code?

thank you

split string into array
set output to first array element
begin loop
is length output + length next array element > max
Yes - print output and clear output
concat next array element to output
end loop

Dude This is what u want to do right?

1. Read sentences from a text file
2. Print it according to the width setting
right??

Use this Pseudocode:

count=0
				Read Word
				if count+len+1 <= WIDTH 
					print word+ " "
					count+=(len+1)
				else if  count+len<=WIDTH
					print word
					count+=len
				else
					print in new line : word+" "
					count=len+1

Put it in a loop and read until u have words...

And btw, if u know the logic, write a pseudocode to implement.

And for dynamism change the width value..

You can simplify this to two cases as follows:

NB: keep track of how much space is remaining on the current line at all time
split input into array of words
print the first word
for every remaining word:
  if it fits print " " + the word
  else print "\n" + the word

where "it fits" is a function of the length of the word vs the space remaining

YEah. Thats a nice way to do it as well. Implement our pseudocodes and develop your code now...

@stevanity

very simple and great solution worked perfectly. thank you very much

thanks to everyone for the help

Try this pseudo

String[] array = split the string using the space as the delimeter.
String finalResult = "";
Define a string buffer
for loop length of array{

    if(index equals 0 OR finalResult equals ""){
        finalResult = array[i]
    }else{
        if((length of finalResult) + length of (" " + array[index ])<20){
            finalResult += " " + array[index]
        }else{
            append to the string buffer the finalResult with the line break character
            finalResult = ""
        }

    }

    if(index equals (length of array-1)){
            append the finalResult variable to the string buffer
    }


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.