Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 01:57 PM
Member
 
Join Date: Jan 2010
Posts: 34
Rep Power: 0
ShinTec is on a distinguished road
Exclamation need help with the remove method on arrayList
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-08-2010, 02:05 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 703
Rep Power: 2
RamyaSivakanth is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2010, 09:25 PM
Member
 
Join Date: Jan 2010
Posts: 34
Rep Power: 0
ShinTec is on a distinguished road
Default
Here the code for Input class
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-16-2010, 01:39 AM
Member
 
Join Date: Jan 2010
Posts: 34
Rep Power: 0
ShinTec is on a distinguished road
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
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
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

Last edited by ShinTec; 02-16-2010 at 01:52 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-16-2010, 09:02 AM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 703
Rep Power: 2
RamyaSivakanth is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-16-2010, 09:38 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
The Contact class needs to have the equals( ... ) and hashCode() implemented.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a method when using an arraylist? Jamison5213 New To Java 10 01-23-2010 08:47 PM
Purse Class ArrayList using for each loop for reverse, transfer method CEgan New To Java 0 12-11-2009 10:26 PM
How to remove “Input method” submenu selection from StyledText context menu? Mitja SWT / JFace 0 09-21-2009 11:33 AM
How can I call my database read method to display its ArrayList? matpj New To Java 3 01-29-2009 10:20 AM
how does the remove method work for sets and hashsets haridharna Advanced Java 4 08-06-2007 12:48 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:04 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org