-1

I'm trying to print out what I inputted for my ArrayList but I keep getting weird results such as this [player@42a57993, player@75b84c92, player@6bc7c054] when I clearly put in legitimate names. So take a look at my code and see what the problem might be:

if (o == 5) {
    double RD, t, old, x;
    String tournament, player;
    int a, number_of_players, place;
    ArrayList<player> players = new ArrayList<player>();
    System.out.println("1:Add a tournament \t2:View Existing");
    a = keyIn.nextInt();
        if (a == 1) {
            System.out.println("\nEnter tournament name");
            tournament = keyIn.next();
            System.out.println("\nEnter number of players");
            number_of_players = keyIn.nextInt();
            System.out.println("Enter players");
            for (int i = 0; i < number_of_players; i++) {
                String name = keyIn.next();
                player plr = new player();
                plr.setName(name);
                players.add(plr);
            }
            System.out.println("Enter places for");
            System.out.println(players);
            place = keyIn.nextInt();

Here is my player class:

public class player {

    private static String name;

    public void setName(String pName)
    {
        name = pName;
    }

    public String getName()
    {
        return name;
    }
}

Let me know what you guys come up with! Thanks!

2 Answers2

6

What you see is the output of the default implementation of toString. It's the class name followed by @ and the unsigned hexadecimal representation of the hash code.

To change that, implement a toString method in your Player class, for example:

public class Player {

    private String name;

    public void setName(String pName) {
        name = pName;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Player{name='" + name + "'}";
    }
}
janos
  • 120,954
  • 29
  • 226
  • 236
  • Okay so when I do this I'm getting what I'm looking for except now it's just taking my last input and repeating it for however many players there are supposed to be. – Kevin Lewis Dec 15 '14 at 21:36
  • I don't think so. Notice that I changed your class a little bit: I made the `name` field non-static. In your original code, you used `static`, which means all object instances share that variable, that's why they are all the same. If you do like in my answer, it should work better. – janos Dec 15 '14 at 21:39
  • 1
    Oh I didn't notice that! My mistake! Thank you! – Kevin Lewis Dec 15 '14 at 21:45
1

Java has no clue about local variable names after the item is added. When you run Collections.toString, it will simply call the toString method of all instances in the ArrayList.

You should override the toString method:

public class player {

    private static String name;

    public void setName(String pName) {
        name = pName;
    }

    public String getName() {

        return name;
    }

    @Override
    public String toString () {
        return name;
    }

}

The toString method specifies a textual representation of Player instances. The default toString method is printing the class of the instance as well as its location in memory.

There are of course alternative ways to produce a textual representation.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555