Link to home
Start Free TrialLog in
Avatar of AttilaB
AttilaB

asked on

Issue with word wrapping contents of text file in Java

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 [1]) 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.
PrintingNeatly.java
kubla-kahn.txt
magna-carta.txt
output.log
Avatar of mccarl
mccarl
Flag of Australia image

The problem is that in the input text the [1] lines are "indented" by a number of spaces. When you split the line based on space as the delimter (at line number 32), Java will populate the returned array with empty strings between each space found. Consider the following examples...

"Hello world".split(" ");   // Will return {"Hello", "world"} as expected

"Hello   world".split(" "); // Will return {"Hello", "", "", "world"} since there are three spaces between the words

"   Hello world".split(" "); // Will return {"", "", "", "Hello", "world"}

Open in new window


So you should probably test each entry in your   wordList   as you iterate through it and ignore any empty strings. There are probably other ways to do it too though.
Avatar of AttilaB
AttilaB

ASKER

I cannot modify the input file, though. It needs to work with this input file, unfortunately.

What I would like to figure out is how to modify the source code so that it even works with the input file that contains the extra spaces and ignores those spaces and puts in one space.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AttilaB

ASKER

Well, actually did this yesterday before I posted this on EE, and it didn't seem to match the result file given for the teacher, that I gave you in the output.log file.

Now that you are telling me the same thing, I did the change again:

                if (currentLineLength + eachWord.length() <= lineLength){
                    if (!(eachWord.equals(""))){
                        tempString = tempString + " " + eachWord;
                        currentLineLength = currentLineLength + eachWord.length()+1;  // accounting for added " "
                    }
                }

Open in new window


And it looked right again, but not like the teacher's solution file. So i used LibreOffice Writer to verify the first
discrepancy location and sure enough my line was 80 characters and the log file's line was shorter!

See screen shot:
User generated image
And the first file happens to come out the same in the end, but there is a line discrepancy:

User generated image
So, it is all working in my program now, the teacher's log file is obviously wrong.

Thanks for leading me to the solution.
Avatar of AttilaB

ASKER

Thanks for your help.
You're welcome!