Justifying Text in Java
Author |
Message |
scottyrush13
|
Posted: Thu Feb 23, 2006 8:52 am Post subject: Justifying Text in Java |
|
|
For class I am to make a simple program that reads text from a file and reformats it so the lines are no longer than a given maximum length, and right justified with a ragged left, and then store that to another file + display it on the screen.
I've got everything except for the right justified part.
How do you make java (and we're using ready to program) justify your text to the right side of the screen? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Thu Feb 23, 2006 2:56 pm Post subject: (No subject) |
|
|
Ok, first of all, you need File IO, which I assume you know cause you said "I have got everything except for the justification of the text". So, now you have all the words from the text file into an array.
Make an int max, which is -1.
Then you run a loop from i = 0; to i < array.length
max = Math.max(max, array[i]);
Now you know what the max length is among all strings.
Now you run another loop i = 0; i < array.length. I am not gonna tell you the entire solution but wil give you a hint. The number of spaces you add to the strings whose length is less than max is (max - array[i].length());
For example: if you have an array.
array[0] = "ASD"
array[1] = "ASDF"
max length is 4 of "ASDF".
so when you encounter "ASD" you would justify it (max - array[0].length()) spaces which is (4 - 3) = 1 |
|
|
|
|
|
|
|