Solved

Issue with word wrapping contents of text file in Java

Posted on 2014-11-17
6
401 Views
Last Modified: 2014-11-17
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
0
Comment
Question by:AttilaB
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 40448711
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.
0
 

Author Comment

by:AttilaB
ID: 40448747
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.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40448759
I cannot modify the input file, though. It needs to work with this input file, unfortunately.
You must not have read comment properly then, as I didn't say to modify the input document.


So you should probably test each entry in your   wordList   as you iterate through it and ignore any empty strings.
"wordList" is a List variable that is in YOUR code, yeah. Well each time you get the next word from your list, check to see if it is empty and if so, just ignore it and go on to the next word.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AttilaB
ID: 40448960
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:
Line discrepancy, first occurance in the log file
And the first file happens to come out the same in the end, but there is a line discrepancy:

Other file first line discrepancy.
So, it is all working in my program now, the teacher's log file is obviously wrong.

Thanks for leading me to the solution.
0
 

Author Closing Comment

by:AttilaB
ID: 40448965
Thanks for your help.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40448975
You're welcome!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now