I am trying to word wrap a body of text using Java.
This function should pass in a String array (each string in the list is a word from the file) and an integer called lineLength (which represents the max number of characters per line, including spaces). This function should return a String that contains the entire text from the file as one string with newline characters. It should not end with a blank line. For testing purposes, lineLength should be equal to 80, as I have done in my main method.
I am testing my word wrapping function with two text files: kubla_kahn.txt and magna_carta.txt. I am saving the output to a file called output.txt (which is generated at runtime). I am comparing the output of output.txt (the word wrapped output file that I am generating when the function is called) to the expected results of the word wrapping output in output.log and writing my function so that the word wrapping output (word wrapped lines) matches the one in output.log.
When I use kubla_kahn.txt to test my function, it works perfectly and matches the expected output for the text file in output.log. However, I seem to be having some issues when running it with magna_carta.txt. If you look at the console and the output.txt file that is generated, the generated output matches the expected output (as seen in output.log) in lines 1-17. However, the word wrapping output does not match the output in the output.log file after line 18. Each time there is a number in square brackets (ie ) found in the string, it adds extra spaces before the bracketed number. I am not sure why that is happening when my algorithm only adds one space after each word.
I have attached the source code (Print_Neatly.java), the two text files that are to be used for testing (kubla_kahn.txt and magna_carta.txt), and the output.log file. This source code will only run in Java 7, so be sure to have that installed.
What is wrong with my code? How can I fix my function so that it does the word wrapping correctly for both files and matches the expected output?
Thank you for your help.