Results 1 to 6 of 6
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation need help with the remove method on arrayList

    need help with arraylist remove my Contact object that i created
    Java 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
    Java 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
    Java 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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    Hi,
    U have nopt posted the ContactInput class.
    Paste the code with code tag.
    Another thing u remove like this might be inputing element may have space...

    Just check like this below
    ----------------------------
    get the indexOf input.getStrInput1().trim().
    Then remove... Might be the string value is having variation by added spaces.
    contacts.remove(input.getStrInput1().trim());
    Ramya:cool:

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    Here the code for Input class
    Java Code:
    import java.util.*;
    
    public class ContactInput 
    {
    	//attributes
    	Scanner keyboard = new Scanner(System.in);
    	private String strInput1,strInput2,strInput3;
    	private int intInput;
    	
    	//constructor
    	public ContactInput()
    	{
    		
    	}
    	
    	//get set method
    	public void setStrInput1()
    	{
    		this.strInput1 = keyboard.next();
    	}
    	public String getStrInput1()
    	{
    		return this.strInput1;
    	}
    	//------------------------------
    	public void setStrInput2()
    	{
    		this.strInput2 = keyboard.next();
    	}
    	public String getStrInput2()
    	{
    		return this.strInput2;
    	}
    	//------------------------------
    	public void setStrInput3()
    	{
    		this.strInput3 = keyboard.next();
    	}
    	public String getStrInput3()
    	{
    		return this.strInput3;
    	}
    	//---------------------------------
    	public void setIntInput()
    	{
    		this.intInput = keyboard.nextInt();
    		
    		if(intInput < 0)
    		{
    			intInput = 0;
    		}
    	}
    	public int getIntInput()
    	{
    		return this.intInput;
    	}
    	//---------------------------------
    	
    }
    alright ill give what you mention a try:)

  4. #4
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation

    nah did nothing eh
    here my updated code eh
    tried to use contain method but didnt work either
    the excersize in the book state that The search should
    find any contact where any instance variable contains a target search string
    for example if "elmore" is the search target then any contact where the first name last name ,email address contains "elmore" should be returned for display or deletions what im i doing wrong:confused:
    Java 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","Search & remove Contact","Display all Contacts","Search & diplay contact","Exit program"};
    	private boolean value = false;
    	private String s;
    	private int i;
    	
    	
    	//constructor
    	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);
    			menu.displayText();
    			try
    			{
    				input.setIntInput();
    				switch(input.getIntInput())
    				{
    				 case 1 : contactAdd();
    				 		  break;
    				 case 2:  contactRemove();
    				 		  break;
    				 case 3:  contactDisplayAll();
    				 		  break;
    				 case 4:  contactSearch();
    				 		  break;
    				 case 5 : menu.pgrmShutdwn();
    				 		  break;
    				 default : inputTest();
    				}
    			}
    			catch(InputMismatchException ime)
    			{
    				secondChance();
    			}
    			catch(Exception e)
    			{
    				System.out.printf("Error occured\n ");
    				System.exit(1);
    			}
    	
    		}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.setStrInput3();
    				
    				contacts.add(new Contact(input.getStrInput1(),input.getStrInput2(),input.getIntInput(),input.getStrInput3()));
    		
    	}
    	
    	//-------------------------------------------------------
    	public  void contactRemove()
    	{
    		System.out.printf("Please enter first,name or email  \n");
    		input.setStrInput1();
    		setS(input.getStrInput1());
    		
    		for(int j = 0;j < contacts.size();j++)
    		{
    			
    			if(contacts.contains(getS()))
    			{
    				setI(contacts.indexOf(getS()));
    				contacts.remove(getI());
    			}
    			/*else
    			{
    				System.out.printf("Contact not found \n");
    			}*/
    		}
    	}
    
    	
    	//--------------------------------------------------------
    	public void contactDisplayAll()
    	{
    		for(Contact c : contacts)
    		{
    			System.out.println(c.toString());
    		}
    			
    	}
    	//-----------------------------------------------------
    	public void contactSearch()
    	{
    		int index;
    		
    			System.out.printf("Please enter the keywords into search\n ");
    			input.setStrInput1();
    			
    			for(index = 0;index < contacts.size();index++)
    			{
    				if(input.getStrInput1().equals(contacts.get(index).getFName()))
    				{
    					System.out.printf("%s",contacts.get(index).toString());
    				}
    				else if(input.getStrInput1().equals(contacts.get(index).getLName()))
    				{
    					System.out.printf("%s",contacts.get(index).toString());
    				}
    				else if(input.getStrInput1().equals(contacts.get(index).getEmail()))
    				{
    					System.out.printf("%s",contacts.get(index).toString());
    				}
    				
    			}
    	}
    	//------------------------------------------------------
    	public void inputTest()
    	{
    		 	if(input.getIntInput() <= 0 || input.getIntInput() >= 5)
    			{
    		 		throw new InputMismatchException();
    			}	
    	}
    	//--------------------------------------------------------
    	public void secondChance()
    	{
    		while(input.getIntInput() <= 0 || input.getIntInput() >= 5)
    		{
    			System.out.printf(" Please insert a number between 1 - 5 \n ");
    			input.setIntInput();
    		}
    	}
    	//--------------------------------------------------------
    	
    	//------------------------------------------------------------
    	public void setS(String s) 
    	{
    		this.s = s;
    	}
    	public String getS() 
    	{
    		return s;
    	}
    	//-------------------------------------------------------------
    	public void setI(int i) 
    	{
    		this.i = i;
    	}
    
    	public int getI() 
    	{
    		return i;
    	}
    	//------------------------------------------------------------
    	
    	
    	
    }
    this is the problem area
    Java Code:
    for(int j = 0;j < contacts.size();j++)
    		{
    			
    			if(contacts.contains(getS()))
    			{
    				setI(contacts.indexOf(getS()));
    				contacts.remove(getI());
    			}
    			/*else
    			{
    				System.out.printf("Contact not found \n");
    			}*/
    		}
    	}
    the if test say that the target string aint in there :confused:
    Last edited by ShinTec; 02-16-2010 at 01:52 AM.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    Hi,
    Your code is not formatted properly and no comments....Its impossible to scan each and everything.First before contains ,try to print the contacts object and try to get the string value what u are willing to remove ....Might be the string not stored propely ..
    try to trim the space and find out..
    Ramya:cool:

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    The Contact class needs to have the equals( ... ) and hashCode() implemented.

    kind regards,

    Jos

Similar Threads

  1. Calling a method when using an arraylist?
    By Jamison5213 in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 08:47 PM
  2. Replies: 0
    Last Post: 12-11-2009, 10:26 PM
  3. Replies: 0
    Last Post: 09-21-2009, 11:33 AM
  4. Replies: 3
    Last Post: 01-29-2009, 10:20 AM
  5. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 PM

Posting Permissions

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