Solved

Issue with word wrapping contents of text file in Java

Posted on 2014-11-17
6
397 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 51
groovy example issue 10 66
@SBGen Method 3 25
Problem to start Neon 20 53
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 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

12 Experts available now in Live!

Get 1:1 Help Now