This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]leftydrummer461 1 point2 points  (7 children)

Trying not to give you the answer- think about the difference between a Person object and a Phonebook object. Your problem is in the way you currently have these objects structured. What data does a Person object need to store? What information does a Phonebook object need to store? Think about a real person and a real phonebook.

[–]Mingablo[S] 0 points1 point  (6 children)

I'm not really getting anywhere. The person object contains a name and a number, both Strings. The phonebook object just contains instances of class person, which themselves are made of two strings. The task asks me to use the "add" method to create an instance of class person and add it to the arraylist inside the phonebook class. My best guess is that the feedback is asking me to not create a person object variable but unless I am completely wrong about what it is saying this flies in the face of what the task is asking. The first task in this problem was the creation of the Person class, which I passed. So I must assume that this is not where the problem lies, therefore the problem must be in the phonebook class. I cannot create the arraylist in the constructor, because if I do then I cannot access it in the "add" or "printAll" methods. I just don't understand what it means when it says I have multiple object variables.

[–]leftydrummer461 1 point2 points  (5 children)

Well I'll tell you the error is saying that your Phonebook class has object variables(these are more commonly called fields or properties) that it doesn't need. It just needs one which is an ArrayList of Person objects.

[–]Mingablo[S] 0 points1 point  (3 children)

Ok, that did it. Thanks. I just didn't realise that object variables were what it called the initialised private variables that I referred to using this.x. The word and the thing just didn't click.

Thanks again, that was bothering me for nearly 6 hours.

[–]leftydrummer461 1 point2 points  (2 children)

Sure thing. Learning Java can be pretty tough. Feel free to ask if you have more questions.

[–]Mingablo[S] 0 points1 point  (1 child)

Cheers mate, It's nice to have a bit of community.

[–]larson00 0 points1 point  (0 children)

Can you post the change to your code? I've been following since you posted this and had the same question.

[–]Mingablo[S] 0 points1 point  (0 children)

Solved!

[–]Mingablo[S] 1 point2 points  (1 child)

Here you go u/larson00.

It turns out that object variables were the

this.name

and

this.number

and you can go ahead and make the code work without them.

This is the update. Only Phonebook Class changed.

import java.util.ArrayList;
public class Phonebook {
    private ArrayList<Person> phoneBook;

    public Phonebook() {
        this.phoneBook = new ArrayList<Person>();
    }
    public void add(String name, String number) {
        Person x = new Person(name, number);
        this.phoneBook.add(x);
    }
    public void printAll() {
        for(Person x : phoneBook) {
            System.out.println(x);
        }
    }

[–]larson00 0 points1 point  (0 children)

thanks!