Search code examples
javaclassconstructorbluej

No suitable constructor found for book error?


Hi sorry beginner coder here and I am not good at explaining things but I needed help and was wondering why I keep getting this error no matter how I format or rearrange the date name and title. so I just was wondering if anyone can help on what order I am suppose put the name dates and titles in? I am using BlueJ compiler.

Here is my code for the issue that I am having:

public BookStore()
{

    inventory = new Book[100];

    inventory[0] = new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
    inventory[1] = new Book(2013, "THE GREAT GATSBY", "F. Scott Fitzgerald");

I keep getting this error no suitable constructor found for Book(java.lang.String,java.lang.String,int,int,int,java.lang.String) constructor Book.Book() is not applicable; (actual and formal arguments list differ in length); constructor Book.Book(Author,Date,java.lang.String) is not applicable (actual and formal argument lists differ in length)

Here is the Book class:

private static final String DEFAULT_TITLE = "Untitled";

private Author author;
private Date published;
private String title;


public Book()
{
    this.author = new Author();
    this.published = new Date();
    this.title = DEFAULT_TITLE;
} // end constructor


public Book(Author author, Date published, String title)
{
    setAuthor(author);
    setDatePublished(published);
    setTitle(title);
} // end constructor


public Author getAuthor()
{
    return author;
} // end accessor


public Date getDatePublished()
{
    return published;
} // end accessor


public String getTitle()
{
    return title;
} // end accessor


public void setAuthor(Author author)
{
    this.author = (null == author ? new Author() : author);
} // end accessor


public void setDatePublished(Date published)
{
    this.published = (null == published ? new Date() : published);
} // end accessor


public void setTitle(String title)
{
    this.title = (null == title ? DEFAULT_TITLE : title);
} // end accessor


public String getAuthorName()
{
    return author.getName().getFullName();
} // end method


public String getDayOfTheWeekBookWasPublished()
{
    return published.getDayOfTheWeek();
} // end method


public void printDetails()
{
    System.out.print(getAuthorName());
    System.out.print("(");
    System.out.print(author.getName().getInitials());
    System.out.print(") wrote ");
    System.out.print(title);
    System.out.print(" on ");
    System.out.print(getDayOfTheWeekBookWasPublished());
    System.out.print(", ");
    System.out.print(Date.getMonthName(published.getMonth()));
    System.out.print(" ");
    System.out.print(published.getDay());
    System.out.print(", ");
    System.out.print(published.getYear());

    Name pseudonym = author.getPseudonym();
    if (null != pseudonym)
    {
        System.out.print(", under the pseudonym ");
        System.out.print(pseudonym.getFullName());
    }

    System.out.println();
} // end method
} // end class

Solution

  • When you want to create a Book object, you need to use one of the defined constructors. You have two options:

    public Book()
    public Book(Author author, Date published, String title)
    

    The first one creates a book with a default author, date and title. The second one receives them as parameters. Assuming the second one is the one you need, you now know:

    1. You need to call new Book(...) with three arguments. Not more, not less.
    2. The first argument has to be an object of type Author. Not a string, not a number, an Author.
    3. The second argument needs to be a Date object.
    4. The third argument needs to be a String.

    Now, here is your call:

    new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
    

    In this call, you pass twelve arguments! And they are just strings and numbers, not an Author, a Date and a String.

    So, you need to create an Author object and a Date object and pass those. For example:

    Author bookAuthor = new Author(...);
    Date   bookDate   = new Date(...);
    inventory[0] = new Book( bookAuthor, bookDate, "Ulysses" );
    

    You can do the same without the extra variables:

    inventory[0] = new Book( new Author(...), new Date(...), "Ulysses" );
    

    Now, you should apply the same logic to the new Author(...) and new Date(...) calls.

    • Check which constructors you have in your code.
    • Find which one is the most suitable for the object you want to create.
    • Pass it the exact number of arguments of the exact type that is defined. If you need, use a call to new ... to create an object of the required type.