This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Appending to StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Appending to StringBuffer " Watch "Appending to StringBuffer " New topic
Author

Appending to StringBuffer

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
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

Joined: Dec 10, 2001
Posts: 7023
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)) ?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Oct 12, 2001
Posts: 1179

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 ]

Regards, Rene Larsen
Dropbox Invite
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Appending to StringBuffer
 
Similar Threads
Array of Objects Paramater to Applet
Unable to locate setter methof for attribute lock in the tag class
PriorityQueue Issue
Comparison failing
inserting strings into another string