0

In editContact on the "[Contact.getName()]" I get the errors:

  • Cannot make a static reference to the non-static method getName() from the type Contact
  • Type mismatch: cannot convert from String to int

Why is this and how can I resolve the issue? thanks!

  public class ContactList
{
private final int MAX_NUMBER_OF_CONTACTS = 100;
private Contact[] contactList = new Contact[MAX_NUMBER_OF_CONTACTS];

public void addContact(String contactName, String contactNumber,
        String contactEmail, String contactAddress)
{
    if (Contact.getContactCount() >= MAX_NUMBER_OF_CONTACTS)
    {
        System.err
                .println("Eror: You have too many contacts, delete one of them you fool!");
        return;

public int editContact(String contactToEdit)
{
    if (editContact(contactToEdit) < 0)

    {
        return -1;
    }       

    else 
        contactList[Contact.getName()].setName("");
        contactList[Contact.getPhoneNumber()].setPhoneNumber("");
        contactList[Contact.getEmail()].setEmail("");
        contactList[Contact.getAddress()].setAddress("");

}
}
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • See [this answer](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java?rq=1) and check google for one of the many tutorials on what static means in java. – Thomas Apr 24 '15 at 16:12

2 Answers2

1

Here:

contactList[Contact.getName()]

You're trying to use a String to access to an index of an array. Array are int index based.

Looks like you need to search for the Contact instance to update in your array. You can do this by using a for loop:

int index = 0;
for (Contact contact : contactList) {
    //use this or another condition
    if (contact.getName().equals(contactToEdit)) {
        //update this instance of contact
        contact.setName(...);
        return index;
    }
    index++;
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

My guess is you are trying to clear the contact details of contactToEdit

int contactIdToEdit = findContactIdForName(contactToEdit);
if (contactIdToEdit < 0)
    return;
Contact c = contactList[contactIdToEdit];
c.setName("");
c.setPhoneNumber("");
c.setEmail("");
c.setAddress("");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130