Problem displaying contents of a class in Java

Posted by LuckySlevin on Stack Overflow See other posts from Stack Overflow or by LuckySlevin
Published on 2010-06-09T12:24:15Z Indexed on 2010/06/09 12:42 UTC
Read the original article Hit count: 161

Filed under:
|
|

My problem is i have a class and in it there is a list of elements of another class.

public class Branch
{
    private ArrayList<Player> players = new ArrayList<Player>();
    String brName;
    public Branch() {}
    public void setBr(String brName){this.brName = brName;}
    public String getBr(){return brName;}
    public ArrayList<Player> getPlayers() { return players; }
    public void setPlayers(ArrayList<Player> players) { this.players =new ArrayList<Player>(players);  }
}
public class Player
{
    private String name;
    private String pos;
    private Integer salary;
    private Integer number;

    public Player(String name, String pos, Integer salary, Integer number)
    {
        this.name = name;
        this.pos = pos;
        this.salary = salary;
        this.number = number;
    }

    public Player(){}

    public String getName() { return name; }
    public String getPos() { return pos; }
    public Integer getSalary() { return salary; }
    public Integer getNumber() { return number; }

    public void setName(String name) { this.name = name; }
    public void setPos(String pos) { this.pos = pos; }
    public void setSalary(Integer salary) { this.salary = salary; }
    public void setNumber(Integer number) { this.number = number; }
}

My problem is to print the players of a Branch with their name,pos,salary,number. For this i tried this simply :

String p1,p2;
int a1,a2;
p1 = input.readLine();
p2 = input.readLine();
a1 = Integer.parseInt(input.readLine());
a2 = Integer.parseInt(input.readLine());
players[0].setName(p1);
players[0].setPos(p2);
players[0].setSalary(a1);
players[0].setNumber(a2);
ptmp.add(players[0]);

myBranch[0].setPlayers(ptmp);

System.out.println(myBranch[0].brName +  "  " + myBranch[0].getPlayers());

I wrote this just to try how to display. I created an array of Players, and Branches so they already defined. The problem is getPlayers() doesn't give me any result. What is the way to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about class