Solved

Issue with word wrapping contents of text file in Java

Posted on 2014-11-17
6
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 40
ejb wildfly example 2 75
Best prediction based on two lists of numbers  in excel 2 74
Running JavaFX on the Raspberry Pi 27 153
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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