Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I run my code to verify if the title or isbn of the book matches with what the user enters, it tells me "cannot invoke get__() on the array type Book []

What I have tried:

//This method will search the books array for a given book
//If the book is found by title or isbn, and the quantity > 0, and it's for sale, it returns the array index of the book
//   otherwise, it returns a -1
public static int searchByTitleOrISBN(Book[] b)
{
    int i = 0;
    //   print a message "Enter the Title or ISBN of the book: "
    System.out.println("Enter the Title or ISBN of the book: ");
    String input = scan.next();


    //   use a while loop to search through the array books using a counter and as long as no match is found
    while (i < b.length) {
    //      if there's a match for the title or a match for the isbn and the book quantity > 0 and the book is for sale
        if ((b.getTitle().equals(title) || b.getISBN().equals(isbn)) & b.getQty() > 0 && b.isForSale()) {
    //         print a message "We have that book in stock and it is available for purchase!!"
            System.out.println("We have that book in stock and it is available for purchase!!");
    //         print column headers "     Title                    ISBN          Quantity       Price          For Sale"
        System.out.println("     Title                    ISBN          Quantity       Price          For Sale");
    //         print the information for the book (call method toString())
        System.out.println(b[i].toString());
    //         indicate a match was found
    }
    //      otherwise,
    else
    //         increase counter i
        i++;
    //      if a match was found,
        if (b.getTitle().equals(title) || b.getISBN().equals(isbn))
    //         return the array index of the book
            return i;
    //      otherwise,
     else {
    //         print a message "Sorry, we do not have that book in stock or it not for sale."
    System.out.println("Sorry, we do not have that book in stock or it not for sale."); }
    }
    //      return -1 (code for book not found, or not in stock, or not for sale)
    return -1;
    }
Posted
Comments
[no name] 5-Dec-21 12:00pm    
It's probably b[i].getTitle() etc.; and not b.getTitle() etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900