简体   繁体   中英

(Java) Array element printing out application.class@xxxxxxx

I am printing out an element from an array. The array holds a (class) Card and each card consists of a string face(in this instance K) and a char(in this instance the spade).The project name is pokergames, the class is Card. Below is what is printing out:

pokergames.Card@26ffd553

Hint: I have tried converting the card using the toString() method and the output is them same as without it.

It should look like this: ♠K

Here is the coding I have used:

//create the 2 private attributes
private String face;
private char suit;

//constructor for card
public Card(String face, char suit)
{
    this.face = face;
    this.suit = suit;
}

//create the public function toString to add the face and suit
public String tostring()
{
    return face + suit;
}

Your toString method signature is wrong. It should be toString and not tostring .

To avoid a logical mistake while overriding a method, you should use @Override annotation to be sure that you are overriding the correct method. So your toString should ideally be like this:

@Override
public String toString() {
    return face + suit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM