Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] strings and objects

Started by omegasyphon Apr 13, 2003 at 3:05 PM 5 replies 300+ views
Original Post
omegasyphon
omegasyphon
a simple java question but i cant seem to figure it out. how do i do this how do i concat an object to a string?
yaroslavd
yaroslavd
Each class extends java.lang.Object. The Object class has a method: public String toString (). This method automatically gets called when you try to do what you are trying to do.

For example:

public class Hi

{
String hey;

Hi () { hey = "Hey"; }

public String toString () { return hey; }

public static void main ( String args[] )

{ System.out.println ( new Hi () ); }
}

This will print out "Hey."
CaptainJester
CaptainJester
You have to iterate through the list yourself and get each one.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
yaroslavd
yaroslavd
Post the code. How are we supposed to help you with your problem if we don''t know what the problem is?
bslayerw
bslayerw
If he is using the Java Collection API then he would have to be using Integer since the Collection API stores everything as Object.

    
Iterator it = myIntegerList.iterator();//could be ArrayList ?

StringBuffer integerString = new StringBuffer();
while(it.hasNext())
{
Integer tmpInt = (Integer)it.next();//this line is to make it a little clean

integerString.append(tmpInt.toString());
integerString.append(" ");//add a space

}

System.out.println(integerString.toString());



something along those lines. I didn't compile it

[edited by - bslayerw on April 20, 2003 12:24:35 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.