need help with arraylist remove my Contact object that i created
|
Code:
|
ArrayList<Contact> contacts = new ArrayList<Contact>(10); |
from my arrayList
i have used the remove methods that are available to you but they dont seem to work, ive also checked the api with no luck either
would i have to make a static method in order for it to remove my Contact object ;
here my code for Contact class
|
Code:
|
public class Contact
{
//attributes
private String fName;
private String lName;
private int phNo;
private String email;
//constructor
public Contact(String fName,String lName,int phNo,String email)
{
setFName(fName);
setLName(lName);
setPhNo(phNo);
setEmail(email);
}
public Contact()
{
}
//get set method
public void setFName(String fName)
{
this.fName = fName;
}
public String getFName()
{
return this.fName;
}
//--------------------------------
public void setLName(String lName)
{
this.lName = lName;
}
public String getLName()
{
return this.lName;
}
//-----------------------------------
public void setPhNo(int phNo)
{
this.phNo = phNo;
}
public int getPhNo()
{
return this.phNo;
}
//--------------------------------------
public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return this.email;
}
//---------------------------------------
public String toString()
{
return String.format("First name : %s Last name : %s Ph no : %d Email : %s\n");
}
}//end of class Contact |
here my code for ContactMethods class
|
Code:
|
import java.util.*;
public class ContactMethods
{
//attributes
ArrayList<Contact> contacts = new ArrayList<Contact>(10);
ContactInput input = new ContactInput();
DisplayMenu menu = new DisplayMenu();
private String [] menu1 = {"Add contact","Remove Contact","Display all Contacts","Search for contact"};
private boolean value = false;
//construct
public ContactMethods()
{
}
//get set methods
//-----------------------------
public void ContactMethod1()
{
do
{
System.out.printf("Welcome to contact program. Please choose from the section below\n ");
menu.setMenuText(menu1);
try
{
input.setIntInput();
switch(input.getIntInput())
{
case 1 : contactAdd();
break;
case 2: contactRemove();
break;
case 3: contactDisplayAll();
break;
case 4: contactSearch();
break;
default : throw new InputMismatchException();
}
}
catch(InputMismatchException ime)
{
System.out.printf(" Please insert a whole number \n ");
}
}while(value != true);
}
//-------------------------------------------------
public void contactAdd()
{
System.out.printf("Please enter first name\n ");
input.setStrInput1();
System.out.printf("Please enter last name\n ");
input.setStrInput2();
System.out.printf("Please enter your phone number\n ");
input.setIntInput();
System.out.printf("Please enter email address\n ");
input.getStrInput3();
contacts.add(new Contact(input.getStrInput1(),input.getStrInput2(),input.getIntInput(),input.getStrInput3()));
}
//-------------------------------------------------------
public void contactRemove()
{
System.out.printf("Please enter first name of contact you wish to delete \n");
input.setStrInput1();
System.out.printf("Please enter last name of contact you wish to delete\n ");
input.setStrInput2();
System.out.printf("Please enter your phone number of contact you wish to delete\n ");
input.setIntInput();
System.out.printf("Please enter email address of contact you wish to delete\n ");
input.getStrInput3();
contacts.remove(input.getStrInput1());
}
//--------------------------------------------------------
public void contactDisplayAll()
{
for(Contact c : contacts)
{
System.out.printf("%s",c.toString());
}
}
//-----------------------------------------------------
public void
} |
any help would be much appreciated and i thank you in advance