0

I'm trying to make two methods. One to add people, and another to view them. This is what I've got so far:

Contacts contactObj = new Contacts();
private void viewContact() {
    System.out.println("Here are your contacts: ");
    contactObj.getNames().forEach(System.out::println);
}
private void addContact() {
    System.out.println("You're about to add " + firstName + " " + lastName + " to your contacts");
    System.out.println("Are you sure? [y]es or [n]o");
    String confirmation = fetchContactDetails.next().toLowerCase();

    if (confirmation == "y" || confirmation == "yes") {
        contactObj.addName(firstName + lastName);
        contactObj.addName("connor template");
    }
     if (confirmation == "n" || confirmation == "no") {
        firstName = null;
        lastName = null;
    }
}

I also have a Contact file that looks like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Contacts {

    private List<String> names;

    public Contacts() {
        this.names = new ArrayList<>();
    }

    //add a name to list
    public void addName(String name) {
        if (!Objects.nonNull(names)) {
            this.names = new ArrayList<>();
        }
        this.names.add(name);
    }

    //get the name attribute
    public List<String> getNames() {
        if (!Objects.nonNull(names)) {
            this.names = new ArrayList<>();
        }
        return this.names;
    }

}

So theoretically I should be able to call addContact() and it should ask me their name and last name. I should then be able to call viewContact() it should display a list of people. Unfortunately it says "Here are your contacts: " with no contacts. Thanks for any help :)

  • 1
    Problem here: `if (confirmation == "y" || confirmation == "yes") {` Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Feb 18 '20 at 11:27
  • 1
    So, instead, consider doing, `if (confirmation.equals("y") || confirmation.equals("yes")) {` – Hovercraft Full Of Eels Feb 18 '20 at 11:27
  • Wow yes, that did work! I thought it was an issue with my Contact class. Thanks a lot –  Feb 18 '20 at 12:31
  • 1
    Note: `Objects.nonNull(names)` is not necessary, because it is never `null` as soon as the constructor is finished. You may remove all instances of `if (!Objects.nonNull(names)) { this.names = new ArrayList<>(); }`. – MC Emperor Feb 18 '20 at 14:29
  • Ok will try. Thanks for all the help :) –  Feb 18 '20 at 14:32
  • 1
    Also note that you are returning the mutable collection field `names` directly. If a method calling `getNames()` adds to or removes something from the returned list, it will be reflected in `names`. It's better to return a new list with the elements, i.e. `return new ArrayList<>(this.names)`. See also [this post](https://stackoverflow.com/questions/29190975/what-is-the-netbeans-return-of-collection-field-optional-warning). – MC Emperor Feb 18 '20 at 14:34

0 Answers0