Question

In the driver, it correctly finds the position, but I tried using contacts.remove(foundPosition) ... And the 'Contact' was not deleted. How can I delete the entire 'Contact' That the user searched for?

Driver:

import java.util.Collections;
import java.util.ArrayList;
import java.util.Scanner;

public class contactDriver
{
public static void main (String[] args)
{                       
           Scanner scan = new Scanner(System.in);

           // Asks you to enter values from the given menu.
           int answer;
           System.out.println ("Press 1 to add a contact.");          // Menu is printed.
           System.out.println ("Press 2 to display all contacts.");
           System.out.println ("Press 3 to search for a contact.");
           System.out.println ("Press 4 to search for a contact to delete.");

           answer = scan.nextInt();

           // ArrayList

           ArrayList<Contact> contacts = new ArrayList<Contact>();

        contacts.add(new Contact("Patrick", "McGee", "334-555-8860", "pmcgee123@gmail.com"));
        contacts.add(new Contact("John", "Appleseed", "142-555-6740", "jappleseed99@gmail.com"));
        contacts.add(new Contact("Matt", "Jordan", "213-555-2323", "mjordan23@gmail.com"));
        contacts.add(new Contact("Kanye", "East", "255-434-9909", "keast123@gmail.com"));
        contacts.add(new Contact("Derrick", "Flower", "144-555-1111", "dflower1@gmail.com"));


               // If statements for the user's decision.

           if (answer == 4){ // User searches for a contact and chooses whether or not to delete the contact.
              System.out.println("Enter the first name of the contact to search for.");
              String fname = scan.next();
              scan.nextLine();

              System.out.println("Enter the last name of the contact to search for.");
              String lname = scan.next();
              scan.nextLine();

              Collections.sort(contacts);
              for(Contact c : contacts)
              System.out.println(c);

              int foundPosition = Collections.binarySearch(contacts, new Contact(fname, lname, "", ""));
              System.out.println(foundPosition);

              // The found contact may or may not be deleted.

              String answer2;
              System.out.println("Would you like to delete this contact? (y/n)");
              answer2 = scan.next();

              if (answer2 == "y"){
              contacts.remove(foundPosition); // **HERE is where I need help.**
              System.out.println ("Contact is deleted. Here is the updated contact list: ");

              for(Contact c : contacts)
              System.out.println(c);
              }

              if (answer2 == "n"){
              System.out.println("Contact will not be deleted.");
              }

              }
              }
              }

Class containing methods:

 import java.util.Collections;
import java.util.ArrayList;
import java.util.Comparator;

public  class Contact implements Comparable<Contact>
{

public Contact(String fname, String lname, String num, String email)
{
  this.fname = fname;
  this.lname = lname;
  this.num = num;
  this.email = email;
}

 public String getFirstname()
 {
  return fname;
 }

 public String getLastname()
 {
  return lname;
 }

  public String getNum()
  {
  return num;
  }

  public String getEmail()
  {
  return email;
  }

  public int compareTo(Contact other)
  {
  if (fname.compareTo(other.lname) == 0)
  return fname.compareTo(other.fname);
  return lname.compareTo(other.lname);
  }

  public String toString()
  {
  return "Contact[" + fname + ", " + lname + ", " + num + ", " + email + "]";
  }

   private String fname;
   private String lname;
   private String num;
   private String email;     

}

Comparator:

import java.util.Comparator;

class ContactComparator implements Comparator<Contact>
{

public int compare(Contact a, Contact b)
{

  if (a.getFirstname().compareTo(b.getFirstname()) == 0)
     return a.getFirstname().compareTo(b.getFirstname());
     return a.getLastname().compareTo(b.getLastname());

}

}

Thanks.

Was it helpful?

Solution

The reason why remove(int) is not working is because is not executed.

In your case is that you wrongly compare reference type.

 if (answer2 == "y"){
    contacts.remove(foundPosition); // **HERE is where I need help.**

When using objects to check that are equal you should call method equals

if ("y".equalsIgnoreCase(answer2)){ // **HERE is where You needed help.**
        contacts.remove(foundPosition); 

You can read more about it here:

How do I compare strings in Java?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top