hi everyone

System.out.println(currentPlayer);

it returns to me like this:Player@7cbde6

i know it was caused by toString() and it is suggested to ues overriding method
but i don't know how to do it, so can somebody teach me how to use overiding method so System.out.println(currentPlayer); will display what i want

thank you very very much

drunkpanda

Recommended Answers

All 5 Replies

What's the signature of toString?
All you need to do is provide a method with that exact signature in your own class.

By reading the API docs for java.lang.Object you can find out the signature of toString.

thank you Jwenting

hellow,
all u hv to do is that override toString() method as one friend also told it, its signature is

public String toString(){
String str;
str = "any thing you want do it here, and place this method in ur class";
return str;
}

Hi khoomy thanks

I have actually done something like this which works ok but what happen if i get mutiple String want to display each individually

public String toString() 
     {
        String thePlayer = currentPlayer.getName();    
        return thePlayer;
    }

for example:
System.out.println(toString()); //which display player's name
System.out.println(somthing else);

Drunkpanda

Where do you get mutiple Strings ? Not clear with your example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.