aspose file tools*
The moose likes Beginning Java and the fly likes stringbuffer and append Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stringbuffer and append" Watch "stringbuffer and append" New topic
Author

stringbuffer and append

Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
Hi all. I'm trying to get an append to work. I'm using eclipse as my java software. so what is to happen is text is entered, then displayed. then each char is check'd to a string to see if there is a match. if there is, the char is appened to a new dummy string so the original string isn't destroyed. if there isn't a match, then the char is converted to hex and appended to the string.

So for example if you type in: this / is a test, the output would be, this %2f is a test. The rest of the code involves changing the space ' ' to a plus. So the whole thing would end up displaying, this+%2f+is+a+test I'm working on doing url encoding for school. Any help would be great. Thanks in advance.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

A StringBuffer is not a String, so you cannot assign result (a StringBuffer reference) to sIndexURL (a variable of type String).

Assemble your String by calling append on the StringBuffer as you're doing now -- but don't try to assign the result to a String variable. Leave it as a StringBuffer until you're done with it. Then when you're ready, call the toString method on your StringBuffer.

[ February 15, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
it seems that your are assigning values of variables that havae different types.


String sIndexURL = "";
sIndexURL = result.append(sEncodedURL); //I get 'cannot convert StringBuffer to String'<--it must be sIndexURL = result.append(sEncodedURL).toString();

indexURL = result.append(cTest); //I get 'indexURL cannot be resolved <--- this one you variable name is indexURL and you declared sIndexURL

sIndexURL = result.append(cTest); //I get 'cannot convert StringBuffer to string' <--like the other it must be sIndexURL = result.append(cTest).toString();


jherald
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jherald Lacambra:
...it must be sIndexURL = result.append(sEncodedURL).toString(); ...

Be careful about assigning intermediate results to the String variable, because you will just be creating new (immutable) String objects each time, defeating the purpose of using StringBuffer.

Leave the "work in progress" as a StringBuffer until you're ready for the String, then call toString. In the original code above, that means don't call toString until you're done with the loop.
[ February 15, 2006: Message edited by: marc weber ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Did you know that you were replicating the built-in class java.net.URLEncoder? There's nothing wrong with writing code for practice, but if there's a built-in class that does exactly what you want then you should generally use it.

(Of course knowing that such code exists is the hard part.)
Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
thanks for the help. i'm going to take a jump on this and see what damage i can do. were not even covering stuff in the books or notes so it makes it a bit hard to figure out at times (and this is a intro calls, LOL).

--Paul, i did know about the urlencoder in .net and it makes life so much easier, but were only in our 3rd week and have to resort to the longer routes right now. kinda stinks but oh well, have to learn somewhere i guess.

Thanks for the help everyone. If I run into a snag I'll post up a bit more.

Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: stringbuffer and append