Results 1 to 6 of 6
  1. #1
    daniel0 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Adding into a complex array

    I have a main class:

    Java Code:
        public class Main {
        public static void main(String[] args) {
    
    
    Player[] playerList = new Player[] {new Player("Daniel", 3, 3, 2, 1, 1, 3),new Player ("Player2", 2, 4, 1, 6, 3, 2)};
    
    System.out.println(Player.getPlayer());
    
        }
    }
    Along with this class
    Java Code:
    public class Player {
    	
        private static String player;
    	   public static String getPlayer() {
    		return player;
    	}
    	private int strength; 
    	   private int defense;
    	   private int magic;
    	   private int resistance;
    	public int getDefense() {
    		return defense;
    	}
    
    	public void setDefense(int defense) {
    		this.defense = defense;
    	}
    	private int skill;
    	   private int luck;
    	   
    	   
    
    	   public int getStrength() {
    		return strength;
    	}
    
    	public void setStrength(int strength) {
    		this.strength = strength;
    	}
    
    	public int getMagic() {
    		return magic;
    	}
    
    	public void setMagic(int magic) {
    		this.magic = magic;
    	}
    
    	public int getResistance() {
    		return resistance;
    	}
    
    	public void setResistance(int resistance) {
    		this.resistance = resistance;
    	}
    
    	public int getSkill() {
    		return skill;
    	}
    
    	public void setSkill(int skill) {
    		this.skill = skill;
    	}
    
    	public int getLuck() {
    		return luck;
    	}
    
    	public void setLuck(int luck) {
    		this.luck = luck;
    	}
    
    	public void setPlayer(String player) {
    		this.player = player;
    	}
    
    	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;}
    	       @Override
    	       public String toString() {
    	           return player + "" + strength + " " + defense + magic + " " + resistance + " " + skill + " " + luck;
    
    
    		
    	
    
    	       }
    }
    and whenever I run it, it prints out the last player, so I was wondering how to identify them separately, but still use the array, as (I believe) it is the most efficient way.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Adding into a complex array

    The problem, I believe, is with the player variable. It is static. Which means that it will have the same value for all instances of the class Player. I recommend that you remove the static declaration of player and getPlayer() and access those fields in a non-static way. I also recommend you read up on static values and method here --> Understanding Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Adding into a complex array

    I recommend that you remove the static declaration of player and getPlayer() and access those fields in a non-static way.
    I'd go a step further and suggest that you consider removing the player variable and getPlayer() method altogether. What is the player field of a Player instance supposed to represent? (I understand that a Player instance is supposed to represent a player. But that player's player instance variable, what is that? Or a static Player's player variable, what is it? A player has magic, luck etc, but what is it for a player to have player). Every instance or static member variable should have a meaning that is documented.

    "static" has its uses (or it wouldn't be in the language, I guess). But a good rule of thumb when you are starting out is not to use it except for the main() method. It certainly isn't needed for constructing a bunch of instances, putting them into an array and printing one or more members of that array.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Adding into a complex array

    Removal is rather rash, then there will be no way to know the name of the player.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    daniel0 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Adding into a complex array

    I figured it out, I was using setters instead of getters.

    The player class now looks like this:
    Java Code:
    public class Player {
    	
        private int defense;
        private int strength;
        private int magic;
        private int resistance;
        private int skill;
        private int luck;
        private String name;
    
        public Player(String name, int defense, int strength, int magic, int resistance, int skill, int luck) {
            this.defense = defense;
            this.name = name;
            this.strength = strength;
            this.magic = magic;
            this.resistance = resistance;
            this.skill = skill;
            this.luck = luck;
            
       }
    
       public int getDefense() {
            return this.defense;
       }
    
       public String getName() {
            return this.name;
       }
       public int getStrength(){
    	   return this.strength;
       }
       
       public int getMagic(){
    	   return this.magic;
       }
       public int getResistance(){
    	   return this.resistance;
       }
       public int getSkill(){
    	   return this.skill;
       }
       public int getLuck(){
    	   return this.luck;
       }
    
    	       @Override
    	       public String toString() {
    	           return name + "" + strength + " " + defense + magic + " " + resistance + " " + skill + " " + luck;
    
    
    		
    	
    
    	       }
    }
    and my main class looks like this

    Java Code:
    public class Main {
        public static void main(String[] args) {
    
    
    Player[] playerList = new Player[] {new Player("Daniel", 3, 3, 2, 1, 1, 3),new Player ("Player2", 2, 4, 1, 6, 3, 2)};
    
    
    System.out.println(playerList[0].getName());
        }
    }
    Thank you for your help though.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Adding into a complex array

    Quote Originally Posted by gimbal2 View Post
    Removal is rather rash, then there will be no way to know the name of the player.
    I should read more closely! player is a String, not a Player...

    @OP: I think your code is looking better now. name can be set and got just like the other qualities of a Person instance as you do in your revised code. Using name for a name also helps careless people like me...

Similar Threads

  1. Adding to an array
    By HarleyRowland in forum New To Java
    Replies: 6
    Last Post: 02-12-2013, 04:15 PM
  2. How complex is too complex for a single class?
    By manji51 in forum AWT / Swing
    Replies: 7
    Last Post: 07-08-2011, 07:20 PM
  3. Help required with complex method: adding item
    By nignogs in forum New To Java
    Replies: 14
    Last Post: 05-11-2011, 01:06 PM
  4. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  5. Array always adding to 0???
    By Javawizard in forum New To Java
    Replies: 6
    Last Post: 11-13-2010, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •