• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java: Full justify text algorithm?

screw3d

Diamond Member
My homework assignment requires me to read a text file and manually justify it to left, center and full.

So far I've been able to do left and center, but I couldn't figure out how to do full justification.

BTW I used StringTokenizer to split text in words.

Anyone, the algorithm, or even better, a sample code?

TIA !!
 
Don't have time to get you the code, but here is the hint that will get you in the right direction.
The area you are print on has a specific size. You have the complete text message in some text reader I presume.

So get the size of the print area, calculate how much text to grab from the reader and print it.
repeat until done. Remember, you want to pad with spaces in FRONT of your text not at the back. so your
method to grab the text from the reader should end with the last character of a word and not on a space.

Best of luck!
 
1. Take the total collumn space of each line which is equal to the number of characters that will fit on a line.
2. Minus the number of characters in the words on that line.
3. Divide that by the number of words on that line minus 1
4. This gives you the number of spaces to append to each word EXCEPT for the last one.


Hope that helps if not PM me
 
Back
Top