Results 1 to 3 of 3
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Thumbs down Cannot find symbol error?

    Hey, just a simple question here...

    I'm doing some exercices of BlueJ and I gotta build the method "join", to join a member to an ArrayList called "members". The thng is that when I compile it sends me the error "cannot find symbol - method add(Membership)"

    I'll pass you the whole code:

    Java Code:
    import java.util.ArrayList;
    
    /**
     * Store details of club memberships.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Club
    {
        // Define any necessary fields here ...
        private ArrayList<String> members;
        
        /**
         * Constructor for objects of class Club
         */
        public Club()
        {
            // Initialise any fields here ...
            members = new ArrayList<String>();
            
        }
    
        /**
         * Add a new member to the club's list of members.
         * @param member The member object to be added.
         */
        public void join(Membership member)
        {
            members.add(member);
        }
    
        /**
         * @return The number of members (Membership objects) in
         *         the club.
         */
        public int numberOfMembers()
        {
            return members.size();
        }
    }
    The "Membership" class has the next code (if you need):

    Java Code:
    /**
     * Store details of a club membership.
     * 
     * @author David J. Barnes and Michael Kolling
     * @version 2006.03.30
     */
    public class Membership
    {
        // The name of the member.
        private String name;
        // The month in which the membership was taken out.
        private int month;
        // The year in which the membership was taken out.
        private int year;
    
        /**
         * Constructor for objects of class Membership.
         * @param name The name of the member.
         * @param month The month in which they joined. (1 ... 12)
         * @param year The year in which they joined.
         */
        public Membership(String name, int month, int year)
            throws IllegalArgumentException
        {
            if(month < 1 || month > 12) {
                throw new IllegalArgumentException(
                    "Month " + month + " out of range. Must be in the range 1 ... 12");
            }
            this.name = name;
            this.month = month;
            this.year = year;
        }
        
        /**
         * @return The member's name.
         */
        public String getName()
        {
            return name;
        }
        
        /**
         * @return The month in which the member joined.
         *         A value in the range 1 ... 12
         */
        public int getMonth()
        {
            return month;
        }
    
        /**
         * @return The year in which the member joined.
         */
        public int getYear()
        {
            return year;
        }
    
        /**
         * @return A string representation of this membership.
         */
        public String toString()
        {
            return "Name: " + name +
                   " joined in month " +
                   month + " of " + year;
        }
    }


    I need to know what's wrong with the method "join", why is it sending me that error.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You've defined your ArrayList to accept objects of type String...so you cannot try to add an object of a different type (Membership) to the List. The fix would be to define your ArrayList generic to be of type Membership (if this is indeed the behavior you want)

  3. #3
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    You've defined your ArrayList to accept objects of type String...so you cannot try to add an object of a different type (Membership) to the List. The fix would be to define your ArrayList generic to be of type Membership (if this is indeed the behavior you want)
    Oh thanks man, I'd not realized about that ;)

Similar Threads

  1. Cannot Find Symbol Error
    By javadummy1 in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 10:13 AM
  2. error cannot find symbol
    By jcoon3 in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 10:56 PM
  3. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  4. 'Cannot find symbol' error
    By minihazard10 in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 04:05 AM
  5. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM

Posting Permissions

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