Search code examples
javaarraysinherited-constructors

array, not applicable for argument


I want to add a Guest to the array guests, can you guys see what am doing wrong, or cant I call a a string, constructor, constructor ?

The error says: the method addGuest(Guest) in the type table is not applicable for the argument ( String, Tea,Cake). I am trying to put a guest in to an array

here is my code.

guests[0]=table.addGuest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));

and constructor of class Guest is like this:

public Guest(String name, Tea newTea, Cake newCake)

constructor of class Tea is:

public Tea(String name, boolean suiker, boolean melk)

Class Cake:

public Cake(String name) {
    this.name = name;
}

addGuest method:

public void addGuest(Guest guest)

and what am trying to do is this:

guests[0]=table.addGuest(new Guest("Alice"),new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));

Solution

  • You should use,

    Guest g = new Guest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));
    table.addGuest(g);
    

    instead of

    guests[0]=table.addGuest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));
    

    because you pass the arguments required for the constructor of Guest to the wrong method(addGuest(<Guest>)), which requires the actual Guest-object.

    EDIT
    Further, this will also not work

    guests[0]=table.addGuest(...);
    

    because table.addGuest(...) is of type void, so it wont return anything, so you will get an compiler error.
    I recommend rethink the use of guests[] , you could probably use a collection (like LinkedList or ArrayList) to solve this problem.