-3

I have a program that currently I'm trying to create a player with customizable integers. I use this object:

public class Player {

   public String player = null; 
   private int Strength=0; 
   private int Defense=0;
   private int Magic=0;
   private int Resistance=0;
   private int Skill=0;
   private int Luck=0;

   public Player(String name, int int1, int int2, int int3, int int4, int int5, int int6) {
       this.player = name;
       this.Strength = int1;
       this.Defense = int2;
       this.Magic = int3;
       this.Resistance = int4;
       this.Skill = int5;
       this.Luck = int6;
   }
}

I use this main class to run it, and set the integer and string values.

public class Main {
public static void main(String[] args) {


Player[] playerList = new Player[] {new Player("Player1", 3, 3, 2, 1, 1, 3),new Player("Player2", 1, 1, 1, 1, 1, 1)};



}

}

However, whenever I attempt to print this, it prints this:Player@659e0bfd I looked through similar questions, but I am not sure where to put the @Override in my program, and if I need to do something different since I have multiple integers, and a string. I only want the string to print out. (Also, is there a way to print out just one part of the playerList, ie. the first section "Player1")

Daniel
  • 13
  • 4
  • Just for stronger typing, you could consider wrapping the ints (Strength, Defense, Magic, etc.) in their own classes (big point of issue with me is that java doesn't have structs) which can handle bad values of data and more strongly require that the programmer pass in Strength, Magic, Defense instances to the constructor – D. Ben Knoble May 26 '15 at 01:09

3 Answers3

2

In the Player class you need to put the override.

Someting like:

public class Player {
    // fields here

    @Override
    public String toString() {
        return Strength + " " + Defense; // plus all the fields here
    }
}

Regarding your other question: yes, you can, but since all the fields are private, you need to create getters for those fields. Then you can do:

public class Player {
    private int defense;
    private String name;

    public Player(int defense, String name) {
         this.defense = defense;
         this.name = name;
    }

    public int getDefense() {
         return this.defense;
    }

    public String getName() {
         return this.name;
    }
}

Player player = new Player(9000);
System.out.println(player.getName + " has defense + " player.getDefense());

Also, it's a good idea to stick to Java conventions. This means that variables are written with a lower case, like strength instead of Strength.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
2

What you are seeing is the output of the toString method in the Object class, which your Player class implicitly extends. If you wish for it to output something else, you must override the toString() method in your Player class.

For instance, add something like this:

@Override
public String toString() {
  return "Player(Strength: " + Strength + ", Defense: " + Defense + ")";
}
JBKM
  • 116
  • 5
0

So basically i assume u use the System.out.println(); method to display ur Object right? You defined your own class and objects. You have to define your own print method, which you have to override. The method is called

@Override 
public String toString(){ return this.Strength + " " + this....);

This method will be called automatically if you insert your object in your System.outprintln();

AngularLover
  • 364
  • 1
  • 12