Results 1 to 6 of 6
- 02-08-2010, 02:57 PM #1
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);
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
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 }
- 02-08-2010, 03:05 PM #2
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:
- 02-08-2010, 10:25 PM #3
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; } //--------------------------------- }
- 02-16-2010, 02:39 AM #4
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; } //------------------------------------------------------------ }
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"); }*/ } }
Last edited by ShinTec; 02-16-2010 at 02:52 AM.
- 02-16-2010, 10:02 AM #5
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:
- 02-16-2010, 10:38 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Calling a method when using an arraylist?
By Jamison5213 in forum New To JavaReplies: 10Last Post: 01-23-2010, 09:47 PM -
Purse Class ArrayList using for each loop for reverse, transfer method
By CEgan in forum New To JavaReplies: 0Last Post: 12-11-2009, 11:26 PM -
How to remove “Input method” submenu selection from StyledText context menu?
By Mitja in forum SWT / JFaceReplies: 0Last Post: 09-21-2009, 12:33 PM -
How can I call my database read method to display its ArrayList?
By matpj in forum New To JavaReplies: 3Last Post: 01-29-2009, 11:20 AM -
how does the remove method work for sets and hashsets
By haridharna in forum Advanced JavaReplies: 4Last Post: 08-06-2007, 01:48 PM
Bookmarks