0

I am working on a address book program and I'm running into some issues. I'm really new to java especially ArrayLists/File I/O and for some reason methods with parameters confuse me a lot.

So here is my issue...Right now I'm working on allowing the user to delete a contact but it doesn't seem to be removed. After removing the contact I "view" the address book all the contacts are printing. I realized I have to rewrite to the text file after the contact is removed but that's not working wither. I think it has to do with how I'm writing to the text file and how I'm adding the contact but I'm really new to ArrayLists/File IO and for some reason methods with parameters get me very confused. Thanks for any help I'm pulling my hair out looking for answers.

I put all my code so you can see how it's set up and put ****'s around each part I think the issue may be coming from. (Contact class is your basic getter and setter and constructors.)

public class AddressBook implements Serializable{

private ArrayList<String> newBook = new ArrayList<String>();
private String dataFile;
private ArrayList<Contact> card =new ArrayList<Contact>(50);
private Contact[] contacts;
private int size = 0;
private int capacity = 0;
private String firstName;
private String lastName;
Scanner scan = new Scanner(System.in);
private Contact temp;

public static void main(String[] args) {
    AddressBook AB = new AddressBook();
    AB.addressBookMenu();       
}

//Method for address book menu
public void addressBookMenu() {
    String option = "";
    System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
    System.out.println("\t add   --> Add a new contact ");
    System.out.println("\t find  --> Find a contact ");
    System.out.println("\t edit  --> Edit an existing contact ");
    System.out.println("\t view  --> View the current address book");
    System.out.println("\t save  --> Save the current address book");
    System.out.println("\t quit  --> quit");
    System.out.println();
    option = scan.nextLine();

    while(!(option.equalsIgnoreCase("quit"))) {
        Contact con = new Contact(firstName, lastName);

        if(option.equalsIgnoreCase("add")) {
            System.out.println("Enter First Name: ");
            String tempFirst = scan.nextLine();
            System.out.println("Enter Last Name: ");
            String tempLast = scan.nextLine();

            con.setFirstName(tempFirst);
            con.setLastName(tempLast); 
            card.add(con);  
            writeContact();
        }   

******************************************************************************
        //View address book
        if(option.equalsIgnoreCase("view")) {
            System.out.println("ADDRESS BOOK: " + "\n" +
                    "==========================================");
            readContact();
            Collections.sort(card);
            printContacts();
        }

******************************************************************************          
        //Find and view a contact.
        if(option.equalsIgnoreCase("find")) {
            findContact(temp);  
        }

        //Edit contacts
        if(option.equalsIgnoreCase("edit")) {
            editContact();
        }   

        System.out.println();
        System.out.println("\n\nPLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
        System.out.println("\t add   --> Add a new contact ");
        System.out.println("\t find  --> Find a contact ");
        System.out.println("\t edit  --> Edit an existing contact ");
        System.out.println("\t view  --> View the current address book");
        System.out.println("\t save  --> Save the current address book");
        System.out.println("\t quit  --> quit");
        System.out.println();
        option = scan.nextLine();
    }
}    

******************************************************************************  
public void writeContact() {
    try (FileOutputStream out = new FileOutputStream("addressbook.txt")) {  
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(card);
        os.close();     
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

******************************************************************************
public void readContact() {
    try (FileInputStream in = new FileInputStream("addressbook.txt")) {
        ObjectInputStream is = new ObjectInputStream(in);
        card = (ArrayList<Contact>)is.readObject();
        is.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
//Print out contacts 
public void printContacts() {
    for(Contact temp : card) {
        System.out.println(temp);
    }
}

public Contact findContact(Contact t) {
    System.out.println("Enter the LAST NAME of the contact you'd like to find: ");
    String tempLastName = scan.nextLine();
    for(Contact temp : card) {
        if(temp.getLastName().equalsIgnoreCase(tempLastName)) {
            System.out.println("\nCONTACT FOUND:");
            System.out.println(temp);
        }
        else if(temp.getLastName() == null || temp.getLastName().equalsIgnoreCase(tempLastName)) {
            System.out.println("Sorry but " + tempLastName + " is not located in this address book");
        }
    }
    return temp;
}

******************************************************************************  
public void editContact() {
    String editOption;
    System.out.print("HOW WOULD YOU LIKE TO EDIT A CONTACT: ");
    System.out.println("\tdelete  --> Delete a contact");
    System.out.println("\tchange  --> edit or replace a contact");
    editOption = scan.nextLine();

    if(editOption.equalsIgnoreCase("delete")) {
        findContact(temp);
        card.remove(temp);
        writeContact();
******************************************************************************  
    }   
}   
}

EXAMPLE add Enter First Name: scott Enter Last Name: smith

add 
Enter First Name: 
Bob
Enter Last Name: 
jones

edit
delete
Enter Last Name of Contact to delete:
jones

view 
ADDRESS BOOK: 
==========================================
FIRST NAME: Bob LAST NAME: Jones
FIRST NAME: Scott   LAST NAME: Smith

^^^^Still showing removed contact^^^^^

**EXPECTED OUTPUT**
view 
ADDRESS BOOK: 
==========================================
FIRST NAME: Scott   LAST NAME: Smith
3
  • 1
    findContact(temp); should be Contact contact = findContact(temp);, although I'd question the need to use temp at all, as findContact never makes use of it Commented Oct 19, 2015 at 4:14
  • Yeah I get really confused by parameters, I know it sounds stupid as it's 101 stuff. I figured I would use findContact to edit the contacts as well so I didn't have to put the same code in the editContacts() method, but trying to remove the temp contact from findContact() I thought I had to return the value....This assignment has been difficult for me but I have learned a lot piecing it all together. Commented Oct 19, 2015 at 4:27
  • I'd leave findContact to do just that, finding a contact ;) Commented Oct 19, 2015 at 4:28

1 Answer 1

1

When you use findContact, it will return the matching Contact to you. In you existing code, you are ignoring the return result.

Instead, you should be using something like...

public void editContact() {
    String editOption;
    System.out.print("HOW WOULD YOU LIKE TO EDIT A CONTACT: ");
    System.out.println("\tdelete  --> Delete a contact");
    System.out.println("\tchange  --> edit or replace a contact");
    editOption = scan.nextLine();

    if(editOption.equalsIgnoreCase("delete")) {
        Contact contact = findContact(temp);
        if (contact != null) {
            card.remove(contact);
            writeContact();
            readContact();
        }
    }   
}   

You findContact method is also not return what it finds, but is returning some other instance value, which, in my testing was always null, which nothing was getting removed from the List...

public Contact findContact() {
    Contact find = null;
    System.out.println("Enter the LAST NAME of the contact you'd like to find: ");
    String tempLastName = scan.nextLine();
    for (Contact temp : card) {
        if (temp.getLastName().equalsIgnoreCase(tempLastName)) {
            System.out.println("\nCONTACT FOUND:");
            System.out.println(temp);
            find = temp;
            break;
        }
    }
    if (find == null) {
        System.out.println("Sorry but " + tempLastName + " is not located in this address book");
    }
    return find;
}

nb: I took out the parameter because it was just confusing the issue

Sign up to request clarification or add additional context in comments.

12 Comments

oh!!! Ok that makes sense I honestly didn't understand from your comment. Thanks Mad I really appreciate it! You've helped me a lot on a tons of questions I've had, not only that but you usually explain things which helps me learn and I appreciate it more than you know! Just thought you guys should know us dumb noobs may not show appreciation but we are very grateful!
one last question, do I need to readContacts() after I write them?
Technically, no, you don't "need" to re-read the list after you've written it, the data should still be the same
Haha, well you make it look good! Ok cool, thanks again. Give it a couple hours I'll be back with more questions! ;-)
Ok, I think I understand. Thanks again Mad! I'm sure I'll be speaking with you again soon! haha
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.