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?
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;
}
}
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
Code :
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?
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 :/
Re: Problems with ArrayList
Quote:
Originally Posted by
93tomh
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?
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.
Re: Problems with ArrayList
Code :
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.
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.
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
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
Code :
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
Code :
contactList.methodYouWant