• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stringbuffer and append

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic