#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Java - need help


    Hello. I have just coded the following, and somehow it keeps tellig me that my list is an incomptabile type. I tried to delete the field were it occured, and the next .list got the same error, so i think it might be something up in the constructor. The class is gathering info from another class called Player. If u want i can link u that aswell though i dont think thats the problem.


    import java.util.ArrayList;
    public class Team
    {
    private ArrayList<Player> list;

    public Team(String name)
    {
    list = new ArrayList<Player>();
    }

    public void addPlayer(Player player)
    {
    list.add(player);
    }

    public void printPlayers()
    {
    for(String info: list)
    {
    System.out.println(name);
    System.out.println(age);
    System.out.println(score);
    }
    }

    public double calcAverageAge()
    {
    double gen;
    double gennem;
    for(int age: list)
    {
    this.gen = gen + age;
    gennem = gen / list.size();
    }
    return gennem;
    }

    public int calcTotalScore()
    {
    for(int score: list)
    {
    this.score = this.score + score;
    }
    return score;
    }

    public int calcOldPlayerScore(int agelimit)
    {
    for(int age: list)
    {
    if(this.age > agelimit)
    {
    this.score = this.score + score;
    }
    }
    }

    public int maxScore()
    {
    int maximum;
    maximum = get.list(0);
    for(int score: list)
    {
    if(maximum > score)
    {
    maximum = score;
    }
    }
    return maximum;
    }


    }
    I hope that someone can help me
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    my list is an incomptabile type.
    Please post the full text of the error message that shows the statement where the error is located.

    Also please edit your post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    41
    Rep Power
    0
    You'll have to post your full code, difficult to see whats wrong from the above code
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    the error message is simply incomptabile type. it is located at for(String info: list), where list is the word they focus on. When i delete the first method:

    "public void printPlayers()
    {
    for(String info: list)
    {
    System.out.println(name);
    System.out.println(age);
    System.out.println(score);"

    the error will just be located at:

    public double calcAverageAge()
    {
    double gen;
    double gennem;
    for(int age: list)
    {
    this.gen = gen + age;
    gennem = gen / list.size();
    }
    return gennem;
    }

    where for(int age: list) list is the word they focus on. if i remove then it goes down to the next list word, and so on.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Deleted.
    Last edited by NormR; September 15th, 2012 at 02:11 PM.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Please edit your post and wrap the code in code tags.

    Code:
    for(String info: list)
    What type of objects are contained in list? The compiler doesn't think they are String.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    public class Player
    {
    private String name;
    private int age;
    private int score;

    public Player(String name, int age)
    {
    score = 0;
    this.name = name;
    this.age = age;
    }

    public String getName()
    {
    return name;
    }

    public int getAge()
    {
    return age;
    }

    public int getScore()
    {
    return score;
    }

    public void setScore(int score)
    {
    this.score = score;
    }

    public void addScore(int score)
    {
    this.score = this.score + score;
    }
    }

    this is the class the previous class is gathering information from. I dont know, do u need more information about the code?.. if u do, plz tell me what, and i will try to explain further more, and i might need some guidance about this forum aswell since I am new on this forum.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Please edit your post and wrap the code in code tags.

    You did not answer this question:
    Code:
    for(String info: list)
    What type of objects are contained in list? The compiler doesn't think they are String.
    The for() statement would try to get String objects from list. If list does not contain String objects, then the compiler gives an error.

    Can you post the code for the definition for the variable: list
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    Well I have just linked the projekt What is gathered from the object is String name; int age; and int score;
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    What is gathered from the object is String name; int age; and int score;
    Those items are contained inside of an object. But the for() statement can NOT get to them.
    It can only get to the objects that are in the list. What is the type of the objects contained in list? Is it Player? If so that is the type that the for() will be able to access from list.
    Change String to Player in the for() statement.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    That surely did help thank u.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    If u still wanna help it says it cant find variable name which i defined in the Player object.

    In team I made a method:

    public void printPlayers()
    {
    for(Player info: list)
    {
    System.out.println(name);
    System.out.println(age);
    System.out.println(score);
    }
    }

    what do i have to type to make it print out name?
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    The variable: info has a reference to the Player object, You need to use that reference to access the variables and methods in the Player object: info.name

IMN logo majestic logo threadwatch logo seochat tools logo