Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problems with ArrayList

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Problems with ArrayList

    I am trying to do a uni project and i have no idea why this isnt compiling, any help would be appreciated.

    errormessage.jpg

    import java.util.ArrayList;
    /**
    * Write a description of class ContactList here.
    *
    * @author Tom Harvey
    * @version 1
    */
    public class ContactList extends Contact
    {
    private ArrayList<Contact> contactList;

    public ContactList()
    {
    contactList = new ArrayList<Contact>();
    }

    public void addPersonalContact(String fName, String sName, String street, String partOnePC, String partTwoPC,int phoneNumber)
    {
    contact.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC));


    }

    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problems with ArrayList

    Hello 93tomh!
    Can you post the Contact class (especially the constructors)? Or better make a small program that executes and shows your problem?

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList

    /**
    * Write a description of class Contact here.
    *
    * @author Tom Harvey
    * @version 1
    */
    public class Contact
    {
    protected String fName;
    protected String sName;
    protected String street;
    protected String town;
    protected String partOnePC;
    protected String partTwoPC;
    protected String postcode;

    public Contact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC)
    {
    this.fName = fName;
    this.sName = sName;
    this.street = street;
    this.town = town;



    this.partOnePC = partOnePC;
    if(partOnePC != null && (partOnePC.charAt(2) > 0 || partOnePC.charAt(3) > 0) && partOnePC.charAt(4) > 0)
    {
    System.out.println("Please insert a string with 3-4 characters");
    }

    this.partOnePC = partOnePC;
    if(partTwoPC != null && (partTwoPC.charAt(2) > 0 || partTwoPC.charAt(3) > 0) && partTwoPC.charAt(4) > 0)
    {
    System.out.println("Please insert a string with 3-4 characters");
    }

    this.postcode = partOnePC + " " + partTwoPC;
    }


    }

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problems with ArrayList

    Since ContactList extends Contact, the first statement in ContactList's constructor should call the superclass (Contact) constructor with the keyword super. In other words, you need to pass the ContactList's constructor at least the arguments that requires the superclass constructor and then have
    super (arguments required for Contact's constructor here);
    Hope that makes sense.
    Also, in the posted code you haven't defined contact (from addPersonalContact method).
    Lastly, can you explain why you need to extend the Contact class?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList

    I have no idea why i have extended the contact class. I never really understood inheritance all that much but i was told i had to use it :/

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problems with ArrayList

    Quote Originally Posted by 93tomh View Post
    I have no idea why i have extended the contact class. I never really understood inheritance all that much but i was told i had to use it :/
    You should't use something because you were told so. You should try to understand why you need it.

    Can you at least explain what are you trying to do? We need more information to help you. Do you have any specific question?

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList

    Ok, so ive took the extends off, looking back at previous homeworks extends was never used for this type of class.
    Anyway, I am trying to make an "address book" and I sort of skipped over array lists in classes because i didnt understand them too well, although i now realise my mistakes. I have encountered a new problem which doesnt look too difficult to solve but ive drawn a blank.


    import java.util.ArrayList;
    /**
    * Write a description of class ContactList here.
    *
    * @author Tom Harvey
    * @version 1
    */
    public class ContactList
    {
    private ArrayList<Contact> contactList;

    public ContactList()
    {
    contactList = new ArrayList<Contact>();
    }

    public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC,int phoneNumber)
    {
    Contact.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC));


    }

    }

    -cannot find symbol - method add(Contact)

    The other code is still the same.
    Sorry for being such a pain, Thanks.

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problems with ArrayList

    Contact.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC));
    With this statement you call the static add method of the Contact class. Obviously, this is not your intention since there is no such method in Contact class - this is why you get the cannot find symbol error.
    I guess you are trying to add a new Contact to the ArrayList. If this is what you want you should consult the API. The ArrayList class has methods for adding elements.
    But that's just a guess. Please explain what you are trying to do with the above statement.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList

    I honestly am not sure. I never really got java when i was in uni and thus failed it and now have this referral project.

    I am trying to add a contact to the array list using the variables in the Contact class.

  10. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problems with ArrayList

    You just need to spend some time reading some basic tutorial on how to use ArrayList. You will find thousands if you just google it.
    Quote Originally Posted by 93tomh View Post
    I am trying to add a contact to the array list using the variables in the Contact class.
    Read again post #8.
    You need to use the ArrayList's add method. Do you know how to call non static methods of a class? - You create a new object of the class (instantiate the class) and then you call it with
    objectOfClass.methodOfClass
    In your code you have created a new ArrayList (contactList). With the above syntax you can call any method you want. Thus, read the ArrayList javadoc, find the method that suits your requirements (in your case the add method) and call it with
    contactList.methodYouWant

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM