0

In this java program I am trying to search if an ISBN number exists in the "Book" type array or not. But when I try to implement the method and display the output, I am getting an error which says " The method searchBook(Book, String) is undefined for the type Book". The <-- HERE comment shows where the error pops up. I do not understand how to rectify this error and any help will be appreciated. Thank you.

The driver class is : QuizMain

public class User {
        
    int ID;
    String name;
    String email;
    int age;
    String isbn;

    void searchBook(Book[] b, String isbn) {

        for (int i =0;i<6;i++) {
            if (b[i].ISBN == isbn) {
                System.out.println(b[i].title);
            } else {
                System.out.println("ISBN Not Found");
            }
        }
    }
}
public class Book {
    String title;
    String author;
    String ISBN;
    float rating;
    int noOfDays;

    void displayBookDetails() {
        System.out.println("Title\tAuthor\tISBN\tRating"+this.title +this.author + this.ISBN +this.rating);
    }

    // book constructor
    public Book(String title, String author, String ISBN, float rating) {
        this.title = title;
        this.author = author;
        this.ISBN = ISBN;
        this.rating = rating;
    }
}
public class QuizMain {
    public static void main(String[] args) {

        Book[] arr = new Book[6];
        arr[0] = new Book("Vincent la la ","king","2194-5357",6.5f);
        arr[1] = new Book("A man of wisdom","henry","2193-4567",3.2f);
        arr[2] = new Book("Apple Garden","timorthy","2104-3080",1.2f);
        arr[3] = new Book("Sherlock","Arthur","2165-0932",5.5f);
        arr[4] = new Book("Hello John","Tarnia","2134-2342",1.5f);
        arr[5] = new Book("Tarzan","Martin","2111-0564",4.2f);

        for(int i =0;i<arr.length;i++) { 
            arr[i].searchBook(arr[i], "2165-0932"); // <-- HERE
        }
        arr.searchBook(arr[5], "2165-0932"); // <-- HERE

    }
}
1
  • You are getting the error "The method searchBook(Book, String) is undefined for the type Book" because searchBook method is not part of Book class, it is part of User class. And, you are trying to invoke it as if it is part of Book class. Commented Apr 7, 2021 at 11:59

2 Answers 2

2

In the User class the searchBook(Book[] b, String isbn) method accepts an array of Book and a String isbn value. But while calling this method you are passing only one Book object instead of array of Book.

Move the searchBook implementation to QuizMain class and call this by passing array of Book as shown below for a workaround .

searchBook(arr, "2165-0932");

May be you need to concentrate on designing the class and its behavior.

Sign up to request clarification or add additional context in comments.

Comments

2

There are two problems in your code. First one your searchBook function expecting Book type array and string but you are passing it one Book instant i.e arr[i] with string and secondly you have define the searchBook() function in User class and using it with object of Book class. Move your function to Book class. Even you don't need that function in any of Book or User class you can simply define static function searchBook(Book[] books, String isbn) inside your QuizMain class and call it like

searchBook(arr,"8344-3452")

2 Comments

Thank you so much for the help, now it works fine. In the question I am asked to create a method in the User class, that is the reason I did not call it in the driver class.
then you can make that method static in User class and call it without creating object of User class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.