0

I am doing a coding exercise to simulate contacts list in a mobile phone, you can search existing contact and add new contact from system input,however, I ran into some problems dealing with scanners and don't know why: (I would really appreciate your help, so thanks in advance!)

Here is my code: First of all a class for Contacts:

public class Contacts {

private String contactName;
private String phoneNumber;

public Contacts(){

}

public Contacts(String contactName, String phoneNumber) {
    this.contactName = contactName;
    this.phoneNumber = phoneNumber;
}

public String getContactName() {
    return contactName;
}

public String getPhoneNumber() {
    return phoneNumber;
}
}

Then I created a MobilePhone class to hold the contact ArrayList, with addContact() and searchContactByName() method:

public class MobilePhone {

private ArrayList<Contacts> contactList = new ArrayList<Contacts>();

//searchContactByName() method takes the contact name you want to search and 
//return the contact object with that contact name if it's in the list, and return null if the contact is not on the list

public Contacts searchContactByName(String contactName){
        Contacts returnContact = new Contacts();
        boolean contactExist = false;
        for(int i = 0; i < contactList.size(); i++){
            if(contactList.get(i).getContactName() == contactName){
                System.out.println("Contact " + contactName + " found");
                contactExist = true;
                returnContact = contactList.get(i);
                break;
            }
        }

        if(contactExist == true){
            return returnContact;
        }else{
            System.out.println("not found");
            return null;
        }
    }

//addContact2() method use the searchContactByName() method to make sure
//that the contact you add does not already exists in the list

public void addContact2(String contactName, String phoneNumber){
        Contacts newContact = new Contacts(contactName,phoneNumber);
        if(searchContactByName(contactName) != null){
            System.out.println("This contact is already in the contact list.");
        }else{
            contactList.add(newContact);
            System.out.println(contactName + " has been added to the list");
        }
    }

However, in the main method when I try to implement the function:

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

    MobilePhone jolenePhone = new MobilePhone();
    //First add a contact object with contactName "Rish" in the list:
    jolenePhone.addContact2("Rish","1234");

    //Then use searchContactByName() function to search the contact object with name "Rish" in the list:
    jolenePhone.searchContactByName("Rish");
    }
    }

This would work normally with the output at the console: enter image description here

However, if I use the scanner function:

MobilePhone jolenePhone = new MobilePhone();
        //First add a contact object with contactName "Rish" in the list:

        jolenePhone.addContact2("Rish","1234");
        System.out.println("Please enter search name: ");
        String searchName = myScanner.nextLine();
        jolenePhone.searchContactByName(searchName);

Then the searchFunction would return not found for the contact name "Rish" I input through the console:enter image description here

I don't understand why would it make a difference at all whether I pass the contactName parameter "Rish" directly, or I use the scanner.nextLine() function to input from the console, what did I miss here?

Lin Qiao
  • 69
  • 1
  • 1
  • 5

2 Answers2

0

change == in if(contactList.get(i).getContactName() == contactName) to equals(). == is for reference equality, equals is for object equality.

ihsan kocak
  • 1,541
  • 1
  • 17
  • 26
0

dont use == use equals() or equalsIgnoreCase() to ignore case sensitiev

Gusti Arya
  • 1,281
  • 3
  • 15
  • 32