Kenneth Yong
Kenneth Yong

Reputation: 385

Method in the type is not applicable to arguments

I'm a newbie working on a basic class creation tutorial and have been given the below instructions.

1) Create a new class called Book.
2) Create fields for title, author, numPages, and isbn.
3) Create a method called toString that appropriately describes the book.
4) In the file, BookRunner.java:
  a) Declare two book variables
  b) Instantiate two book objects and set their fields.
  c) Print descriptions of the book objects using their toString methods.

So I've been working with two classes as instructed, and you can see the code below

public class Book {
    public String title;
    public String author;
    public String numPages;
    public String isbn;

    public void toString(String bookName) {
        String description = "Title:" + title + "Author"+ author + "Num. of pages" + numPages + "ISBN" + isbn; 
        System.out.println(description);
    }

public class Ex1_BookRunner {

    public static void main(String[] args) {
        Book firstBook;
        Book secondBook;

        firstBook = new Book();
        secondBook = new Book();

        firstBook.title = "One Piece";
        firstBook.author = "Oda-Sensei";
        firstBook.numPages = "100";
        firstBook.isbn = "123456";

        secondBook.title = "Life of Megan Fox";
        secondBook.author = "Micheal Bay";
        secondBook.numPages = "200";
        secondBook.isbn = "098765";

        toString(firstBook);
        toString(secondBook);
    }
}

My code shows no errors until the last two lines where I call the method toString.

I get the error below

The method toString() in the type Object is not applicable for the arguments (Book)

Am I making some fundamental error somewhere in my method declaration? I've looked at other posts on SO with the same question but I have trouble understanding the explanations given because they mostly cover code syntax I haven't learnt yet.

Any help is appreciated :)

Upvotes: 2

Views: 9402

Answers (4)

eli
eli

Reputation: 9228

there are two public classes in the same class

        public class Book {
//your codes or variables
}
    public class Ex1_BookRunner {
//your code or variables
}

Try this

Upvotes: 0

user3575886
user3575886

Reputation:

try our toString method as

 public String toString() {
   String discription= "Title:" + title + "Author"+ author + "Num. of pages" + numPages + "ISBN" + isbn;
    return discription;
}

and your print statement in your main method should also look like this

   System.out.println(firstBook.toString());
   System.out.println(secondBook.toString());

I hope that helps

Upvotes: 0

Jobs
Jobs

Reputation: 3377

Change them to:

firstBook.toString();
secondBook.toString();

You can call the default .toString() method on the firstBook and secondBook that you've created. But since you didn't define a toString method that takes a Book object as a parameter, passing a Book object into toString() produces the aforementioned error.

In fact, your error line explains this quite clearly:

The method toString() in the type Object is not applicable for the arguments (Book)

Upvotes: 0

arcy
arcy

Reputation: 13103

Try firstBook.toString(); - this produces a String, if you want to see it, you can do something like System.out.println(firstBook.toString());. The error is telling you that there is no method toString that takes a book as a parameter. What you want instead is to call the toString() method on the instance of Book that you have created.

Upvotes: 1

Related Questions