Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My file output likes to repeat words.  RSS feed

 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, again...

I have a program that gets a file the user wants to open. Then it formats the file to a width in chars the user wants each line to be.
Example "I love drinking root beer so much its a problem". If the user wanted the lines to be shorter, say 16 chars, the program would start a new line at "root".

output should be something like

I love drinking
root beer so
much its a
problem

I get doubles

II love love
root root beer
beer so so
and so on......

I tweaked this thing so many times and I just cant figure it out. Any advise? Thanks








 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 4. you add word to line. Then in line 5. you add word to formattedLine. Then in line 10. you add line to formattedLine, so you get word twice. You should only add word to formattedLine after you calculate if formattedLine.length() + word.length() is less than width. Then you would add word or line not both.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Ki wrote:Hi all, again...

Steve,

Please don't put very long lines inside code tags; it makes your thread hard to read. I'd correct it myself, except that your entire post is excessively indented.

Please re-read the UseCodeTags page. Thoroughly. And you can change your post with the Edit button.

Thanks.

Winston
 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Steve Ki wrote:Hi all, again...

Steve,

Please don't put very long lines inside code tags; it makes your thread hard to read. I'd correct it myself, except that your entire post is excessively indented.

Please re-read the UseCodeTags page. Thoroughly. And you can change your post with the Edit button.

Thanks.

Winston

Sorry Winston, I edited the indention's. Ill make sure next time its more readable.
 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve, that was a huge help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!