user9664805
user9664805

Reputation:

Why setter methods don't update Object?

I want to update the Contact object, which has 2 fields - name & phoneNumber. Unfortunately I don't know why setter methods don't work.

Firstly I tried with the #1 version of updateContact() method - code below. I think that it may have something with updating reference of an object, not the object itself? I'm not sure.

If anybody could explain me that, why the code with setters doesn't work... - I mean it updates the 'contact' within the function, but doesn't update contact in the ArrayList - contacts.

The #2 method works, but I'm not sure if it's a good idea/solution - well, it works... but I think that with setters it should work as well.

1

    public void updateContact(String name) {

        Contact contact = findContact(name);

        System.out.print("Enter new name: ");
        contact.setName(scanner.nextLine());

        System.out.print("Enter new phone number (9 numbers): ");
        contact.setPhoneNumber(scanner.nextLine());
    }

2

    public void updateContact(String name) {

        Contact contact = findContact(name);

        String replaceName;
        String replaceNumber;

        System.out.print("Enter new name: ");
        replaceName = scanner.nextLine();

        System.out.print("Enter new phone number (9 numbers): ");
        replaceNumber = scanner.nextLine();

        Contact replace = new Contact(replaceName, replaceNumber);

        contacts.set(contacts.indexOf(contact), replace);
    }

findContact method

    public Contact findContact(String name) {

        Contact currentContact = null;

        for (Contact contact : contacts) {
            if (contact.getName().equals(name)) {
                currentContact = new Contact(name, contact.getPhoneNumber());
            }
        }

        return currentContact;
    }

Thanks for help in advance.

Upvotes: 0

Views: 1325

Answers (2)

Kuldip Patil
Kuldip Patil

Reputation: 1

Complete solution by using different class Name.. Above solutions doesn't Continued old Data..

System.out.println("Enter Id OF student");
String inputId = sc.next();
for(Student std : list ) {
   if(std.getId().equals(inputId)){
     Student newdata = std;
     System.out.println("Enter New Marks and Phone Number:");
     Student update = new Student(newdata.getId(),newdata.getCourse(),sc.nextDouble(),sc.next(),newdata.getEmail());
     list.set(list.indexOf(newdata), update);
         break;
  }
}

Upvotes: 0

Islam Hassan
Islam Hassan

Reputation: 712

Your findContact() method is not returning a reference to a Contact object in the ArrayList, instead you are creating a new object with a copy of the data and then returning it.

Change it as follows and your first approach should work:

public Contact findContact(String name) {

    Contact currentContact = null;

    for (Contact contact : contacts) {
        if (contact.getName().equals(name)) {
            currentContact = contact;
            break;
        }
    }

    return currentContact;
}

Upvotes: 2

Related Questions