I have to read in an integer which will be the length of the succeeding lines. (The lines of text will never be longer than the length provided).
I then have to read
I wrote a simple method to justify text. Its not 100% accurate, but works for the most part (since it ignores punctuations completely, and there might be some edge cases missing too). Also, Word justifies text in a richer manner (by not adding spaces to fill up the gap, but evenly distributing the width of a whitespace, which is tricky to do here).
public static void justifyText (String text) {
int STR_LENGTH = 80;
int end=STR_LENGTH, extraSpacesPerWord=0, spillOverSpace=0;
String[] words;
System.out.println("Original Text: \n" + text);
System.out.println("Justified Text: ");
while(end < text.length()) {
if(text.charAt(STR_LENGTH) == ' ') {
// Technically, this block is redundant
System.out.println (text.substring(0, STR_LENGTH));
text = text.substring(STR_LENGTH);
continue;
}
end = text.lastIndexOf(" ", STR_LENGTH);
words = text.substring(0, end).split(" ");
extraSpacesPerWord = (STR_LENGTH - end) / words.length;
spillOverSpace = STR_LENGTH - end + (extraSpacesPerWord * words.length);
for(String word: words) {
System.out.print(word + " ");
System.out.print((extraSpacesPerWord-- > 0) ? " ": "");
System.out.print((spillOverSpace-- > 0) ? " ": "");
}
System.out.print("\n");
text = text.substring(end+1);
}
System.out.println(text);
}
I had to do something similar to this in Java recently. The code itself is relatively straightforward. What I found took the longest, was getting my head around the justification process.
I started by making a step by step process of how I would justify text manually.
Doing this made coding the algorithm much simpler for me!
You said you have read in the line length and the text on the line so 1 and 2 you have already done. With 2 being a simple string.length()
call.
Calculating the number of spaces required to add to the string to equal the line length is simply taking the line length and subtracting the length of the string.
lineLength - string.length() = noofspacestoadd;
There is probably more than one way of doing this. I found that the easiest way of doing this was converting the string into a char[] and then iterating through the characters checking for ' ' and setting a count for when it does find a ' '
This is a simple division calculation!
noofgaps / noofspacestoadd = noofspacestoaddtoeachgap;
Note: You have to make sure you're doing this division with integers! As 5 / 2 = 2.5, therefore you KNOW you have to add 2 spaces to each gap between the words, and divisions using int's truncates the decimal number to form an integer.
Before being able to add the number of strings required to add to each gap, you need to convert this number into a string of spaces. So you need to write a method for converting a given integer into a string of spaces equating to that given number. Again, this can be done in different ways. The way I did it was something like this
String s = "";
for(int i=noofspacestoaddtoeachgap; i>0; i--)
{
s+= " ";
}
return s;
The way I did this was to convert the string into an array of substrings, with the substrings being each word in the array. If you look up the String class in the javadoc you should find the methods in the String class you can use to achieve this!
When you have your array of substrings, you can then add the string of spaces to the end of each substring to form your new substring!
This is again a simple calculation. Using the % operator you can do a remainder division similar to the division we did earlier.
noofgaps % noofspacestoadd = noofspacestoaddtoeachgap;
The result of this calculation gives us the number of extra spaces required to justify the text.
This is probably the most difficult part of the algorithm, as you have to work out a way of iterating through each gap between the words and add an extra space until there are no more extra spaces left to add!
return String;
What I do is split the sentence in to words. Then figure out how many spaces need to be added. Then iterate over the words and add a space to each one until you run out of spaces to add. If you have enough spaces where you need to add more than one to the words (like you have 5 words, but need to add 13 spaces), simply divide the number of spaces left by the number of words, and add that number to each word first. Then you can take the remainder and iterate across the words adding a space until you're done. Also make sure that you only add spaces to all but the last word in the sentence.