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.
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
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 ]
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.)
Joined: Feb 15, 2006
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.