-3

everything is fine except displayMatch() and deleteMatch() the result that showing is "No matches" while input that i have entered has some matches

may this part of the question will help

enter image description here

import java.util.ArrayList;
import java.util.Scanner;
import java.lang.*;


public class ContactDatabase
{
    private ArrayList<Contact> contacts;        // ArrayList of contact
    private static final int QUIT = 0;          // Menu choices
    private static final int ADD = 1;
    private static final int LISTALL = 2;
    private static final int SEARCH = 3;
    private static final int DELETE = 4;
/**
* Default constructor - make a new ArrayList object with parameter type Contact
*/
ContactDatabase()
{
     contacts = new ArrayList<Contact>();
}


/**
* inputContact inputs contact information from the keyboard.
* It then stores this new contact in the contacts ArrayList.
*/
public void inputContact()
{
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter first and last names: ");
    String firstName = input.next();
    String lastName = input.nextLine();
    /*  while (input != null || input != " ") {
            //firstLast = input.nextLine();
        }
    contacts.add(firstName);
    contacts.add(lastName);
    */
    System.out.println("Now enter " + firstName + " " + lastName + "\'s email address: ");
    String emailAddress = input.nextLine();

        if (emailAddress.indexOf('@') < 0) {
            System.out.println("No '@' in email address\n Please enter a correct email address: ");
        }

        if (emailAddress.indexOf('.') < 0) {
            System.out.println("No '.' in email address!");
        } else {
            //emailAddress = input.nextLine();
            System.out.println("Email address accepted!");
        }

    System.out.println("Please enter " + firstName + " " + lastName + "\'s phone number: ");
    String phoneNumber = input.nextLine();

        boolean number = false;
        int n = 0;
        while (n < phoneNumber.length()) {
            char c = phoneNumber.charAt(n);

            if (!(c >= '0' && c <= '9'))
                number = false;
                n++;
        }
        number = true;

        phoneNumber = phoneNumber.replaceAll("\\-","");
        phoneNumber = phoneNumber.replaceAll("\\(","");
        phoneNumber = phoneNumber.replaceAll("\\)","");
    System.out.println("Phone number accepted.");

    Contact newContact = new Contact(firstName, lastName, phoneNumber, emailAddress);
    contacts.add(newContact);
    System.out.println(firstName + "" + lastName + " has been added as a contact.");
}

/**
* displayAll iterates through the ArrayList of contacts and outputs each one
* to the screen.
*/
public void displayAll()
{   

    System.out.println("Your contacts are: ");
    for (Contact c : contacts)
    {   
        if (contacts.isEmpty()) {
            System.out.println("No contacts.");
        } else {
            System.out.println(c);
        }
    }   
}

/**
* displayMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and outputs each one
* to the screen if the contact information contains the keyword.
*/
public void displayMatch()
{

    System.out.println("Enter keyword: ");
    Scanner input = new Scanner(System.in);
    String in = input.next();

        for (Contact c : contacts)
        {
            if (in.equals(c)) {
                System.out.println(c);
            } else {
                System.out.println("No matches.");
            }
        }   
}

/**
* deleteMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and asks the user
* if the contact should be deleted, if the contact information contains the keyword.
*/
public void deleteMatch()
{

    System.out.println("Enter keyword: ");
    Scanner input = new Scanner(System.in);
    String in = input.next();

    for (Contact c : contacts) 
    {
        if (in.equals(c)) {
            System.out.println("Should " + c + " be deleted? (type 'YES' or 'Y')");
            //in = input.nextLine();

            boolean done = false;
            while (! done)
            {   
                if (in.equalsIgnoreCase("yes") || in.equalsIgnoreCase("y")) {
                    contacts.remove(c);
                } else {
                    done = true;
                }
            }   

        } else {
            System.out.println("No matches!");
        }
    }
}

// Main class
public static void main(String[] args)
{
    ContactDatabase cdb = new ContactDatabase();
    Scanner scan = new Scanner(System.in);
    int choice = ADD;

    // Main menu
    while (choice != QUIT)
    {
        System.out.println();
        System.out.println("Choose from the following:");
        System.out.println("0) Quit");
        System.out.println("1) Add new contact");
        System.out.println("2) List all contacts");
        System.out.println("3) Search contacts by keyword and display");
        System.out.println("4) Search contacts by keyword and remove");
        choice = scan.nextInt();
        switch (choice)
        {
            case ADD: cdb.inputContact();
                        break;
            case LISTALL: cdb.displayAll();
                        break;
            case SEARCH: cdb.displayMatch();
                        break;
            case DELETE: cdb.deleteMatch();
                        break;
        }
    }
}

/**
 * The inner class, Contact, stores the details for a single contact.  
 *  There is no error checking on any of the input.  Whatever string is 
 *  passed in for a given attribute is accepted.  
 */
class Contact
{
   private String first, last, phone, email;

   /**
    * Constructors.
    */
   public Contact()
   {
   }

   public Contact(String first, String last, String phone, String email)
   {
      this.first = first;
      this.last = last;
      this.phone = phone;
      this.email = email;
   }

   /*
    * Accessor Methods
    */

   public String getFirst()
   {
      return first;
   }

   public String getLast()
   {
      return last;
   }

   public String getPhone()
   {
      return phone;
   }

   public String getEmail()
   {
      return email;
   }

   /* 
    * Mutator Methods
    */
   public void setFirst(String first)
   {
      this.first = first;
   }

   public void setLast(String last)
   {
      this.last = last;
   }

   public void setPhone(String phone)
   {
      this.phone = phone;
   }

   public void setEmail(String em)
   {
      this.email = em;
   }



   /*
    * Return all fields concatenated into a string
    */
   public String toString()
   {
      return last + ", " + first + ". " + phone + ", " + email;
   }


   public boolean equals(Object otherObject)
   {
      if (otherObject ==null)
      {
         return false;
      }
      else if (getClass() != otherObject.getClass())
      {
         return false;
      }
      else
      {
         Contact otherContact = (Contact)otherObject;
         return (first.equals(otherContact.first) && 
               last.equals(otherContact.last)&&
               phone.equals(otherContact.phone)&&
               email.equals(otherContact.email));
      }
   }

} // end inner class, Contact
} // end class, ContactDatabase
dave
  • 61
  • 6
  • 2
    You fail to understand the difference between == and equals. That's fatal for String comparisons. – duffymo May 01 '16 at 23:12
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – forgivenson May 01 '16 at 23:22

1 Answers1

2

Well, you are comparing the String you read in your displayMatch() method with the elements of your Contacts list:

in.equals(c)

Since a String just can't be equal to a Contact, your list will stay empty.

You could change your equals() method in the Contact class to compare its content to a given String (example for last member:)

public boolean equals(Object otherObject)
   {
      if (otherObject == null)
      {
         return false;
      }
      else if (otherObject instanceof String) {
         String otherString = (String) otherObject;
         String lowerCaseString = otherString.toLowerCase();
         return this.last != null && this.last.toLowerCase().contains(lowerCaseString);
      }
      else if (getClass() != otherObject.getClass())
      {
         return false;
      }
      else
      {
         Contact otherContact = (Contact)otherObject;
         return (first.equals(otherContact.first) && 
               last.equals(otherContact.last)&&
               phone.equals(otherContact.phone)&&
               email.equals(otherContact.email));
      }
   }

and then use

c.equals(in);

instead.

J. Dow
  • 563
  • 3
  • 13
  • i don't understand this part return this.last != null && this.last.toLowerCase().contains(((String)otherObject.toLowerCase())); what do you mean by toLowerCase() and (String) – dave May 03 '16 at 04:20
  • I have broken down the line into single statements. toLowerCase() is just a method to make all characters of a String small (TeXt -> text) Since the equals method accepts an object as input parameter, we have to check the concrete type. If it is a String, we want to handle it like one. This is what the casting operator is doing. – J. Dow May 03 '16 at 07:06