We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Converting an object to a string
Page Index Toggle Pages: 1
Converting an object to a string (Read 634 times)
Converting an object to a string
Aug 21st, 2009, 1:17pm
 
hi all
Is it possible to convert an object to a string? The bit in red below is my effort to do so, but I'm doing something wrong. The rest of the code works okay.
Thanks

Iterator it = nodeTable.entrySet().iterator();
while (it.hasNext()) {
 Map.Entry me = (Map.Entry)it.next();
 if (me.getValue()== selection){
    String article = Integer.toString(me.getKey());
    String url = join("blahblahblah", article);
    link(url, "_new");
  }
}
Re: Converting an object to a string
Reply #1 - Aug 21st, 2009, 1:29pm
 
most Objects have a toString() on them, but sometimes you'll get things that aren't really useful (a classname and a pointer)

you can concatenate objects together with just

String url = "blah blah blah " + me.getKey();

(it'll call the object's toString() automatically)

you also need to be wary of null strings (you'll get "null" in your string). and dereferencing null objects (if me above was null you'd get an exception there).

(and StringBuffer() is better if you're doing a lot of this)
Re: Converting an object to a string
Reply #2 - Aug 21st, 2009, 1:52pm
 
Thanks very much Koogy! Smiley
Page Index Toggle Pages: 1