• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Appending to StringBuffer

 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I definitely feel this is inefficient processing of the String. Since the efficient or at least more succinct way is not coming to mind, I am trying to get this to work. This code is not appending to the StringBuffer. It seems
"indexInPage" is not visible inside the second while. Can you enlighten with me with why this is happening?


Thanks.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried your code yet, but I'm quite certain that you have at least a couple of problems.
1. No return statement, but your method claims to return a String.
2. More important for this discussion: I'd bet that you're gonna get a StringIndexOutOfBoundsException because you're iterating along the characters in the input String but trying to use that same index to reference characters in the StringBuffer (which is likely quite shorter than the String). When you said pageTextContents.append( pageTextContents.charAt(indexInPage)) did you perhaps mean to say pageTextContents.append( pageToProcess.charAt(indexInPage)) ?
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using separate indices cleared it up right away. Why would length of StringBuffer ever be a problem?
Thanks for taking the time to help. I appreciate it. I haven't been coding in a few months and am making some rookie mistakes so this forum is great for me right now. Thanks again.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using separate indices cleared it up right away.
Really? I can't quite imagine how seperate indices would be necessary or useful in solving this problem.
Why would length of StringBuffer ever be a problem?
It looks to me as if the StringBuffer is to be filled with the text that is surrounded by any html tag. Since this is a subset of the input String, it's going to be shorter. When the String's index, indexInPage, moves along the length of the input String, it will become a value that is longer than the StringBuffer. When you try to access a character in the StringBuffer with pageTextContents.charAt(indexInPage) it's likely that the indexInPage will have a value that is too big (i.e. longer than the StringBuffer) and will cause an out of bounds exception.
So what solution did you come up with that works?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the line: pageTextContents = pageTextContents.append( pageTextContents.charAt(indexInPage)); should look like this instead: pageTextContents.append( pageToProcess.charAt(indexInPage));
When you use a StringBuffer the manipulation is done inside the buffer and is not returned.
Rene
[ October 28, 2002: Message edited by: Rene Larsen ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just nitpicking...
When you use a StringBuffer the manipulation is done inside the buffer and is not returned.
Yes, the manipulation is internal, but a reference to the StringBuffer is also returned.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic