找不到适合 get(Book) 的方法

No suitable method found for get(Book)

我正在研究 displayBooksFromAuthor(),它将显示用户输入指定的作者。我试图在没有元素 "author" 的 getter 和 setter 方法的情况下执行此操作。抱歉,如果之前已经发布过,但我找不到有关我的情况的任何信息。感谢您的帮助!

import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class BookCollection
{
    public ArrayList<Book> bookList = new ArrayList<Book>();
/**
    Scanner input = new Scanner(System.in);

    public int counter = 0;
    String name;

    public BookCollection() throws IOException
    {
        String oneLine;
        Scanner fileScan, dataScan;
        String isbn, author, area;
        int pages;
        Book oneBook;

        fileScan = new Scanner(new File("booksTwo.txt"));
        while(fileScan.hasNext())
        {
            oneLine = fileScan.nextLine();
            dataScan = new Scanner(oneLine);
            isbn = dataScan.next();
            author = dataScan.next();
            area = dataScan.next();
            pages = dataScan.nextInt();
            oneBook = new Book(isbn, author, area, pages);
            bookList.add (counter, oneBook);
            counter++;
        }
    }**/

    public BookCollection(){
    bookList.add(new Book("1001-01-141514", "Letones", "CS", 611));
    bookList.add(new Book("1002-01-141424", "Lewis", "CS", 477));
    bookList.add(new Book("1003-01-141434", "Smith", "MATH", 698));
    bookList.add(new Book("1004-01-141444", "Smith", "CS", 617));
    bookList.add(new Book("1005-01-141454", "Brown", "CHEM", 326));
    bookList.add(new Book("1006-01-141464", "Smith", "BIO", 127));
    bookList.add(new Book("1007-01-141474", "Sanket", "CS", 998));
    }

    public String toString()
    {
        String s = "\n All Books in the store \n\n";
        for(int i = 0; i <bookList.size(); i++)
            s += bookList.get(i).toString() + "\n";
        return "" + s;
    }

    public void displayLongBooks()
    {
        System.out.println("LONG BOOKS \n");
        for (Book b: bookList)
        {
            if (b.isLong())
                System.out.println(b);
        }
    }

    public void displayBooksFromAuthor(String author)
    {
        for(Book b: bookList)
        {
            if (bookList.get(b) == author);   //error here
            System.out.println(b);
        }
    }
}

错误在下面方法的第 4 行

public void displayBooksFromAuthor(String author)
{
    for(Book b: bookList)
    {
        if (bookList.get(b) == author);   // error is here
        System.out.println(b);
    }
}

你应该简单地使用b.author.equals(author)来比较作者姓名。而且,你必须使用 equals() 方法来比较字符串!此外,不要在 if 语句的末尾放置分号(;),除非需要。它将始终打印下一行。

更正为:-

public void displayBooksFromAuthor(String author)
{
    for(Book b: bookList)
    {
        if (b.author.equals(author))
            System.out.println(b);
    }
}

你的问题出在这段代码中:

/*
   Your original code:
 */
public void displayBooksFromAuthor(String author)
{
    for(Book b: bookList)
    {
        if (bookList.get(b) == author); // WRONG
        // 1. You can't compare strings with ==
        // 2. Why do you put an ending ; there? If you leave it there
        //    the if statement is useless
        System.out.println(b);
    }
}

您已经从 bookList 对象中提取了一本书 b,所以您需要做的就是使用 b。此外,您不能将字符串与 == 进行比较;要么使用 compareTo() or equals():

/*
   Corrected code:
 */
public void displayBooksFromAuthor(String author) {
    for(Book b : bookList) {
        if(b.author.equals(author))
            System.out.println(b);
    }
}

或者,您可以使用:.equalsIgnoreCase() or .compareTo() or .compareToIgnoreCase():

  • if(b.author.equalsIgnoreCase(author)){/*Code if true*/}
  • if(b.author.compareTo(author) == 0) {/*Code if true*/}
  • if(b.author.compareToIgnoreCase(author) == 0) {/*Code if true*/}

此外,AFAIK 将对象属性保密并使用 getXXX() 方法来读取它们是更好的做法。所以,在你的 Book class 中,你应该这样写:

public class Book {
    // Attributes
    private String author
    // More attributes
    /**
     * Constructor
     */
    public Book(/*Constructor arguments*/) {
        // Initialization code
    }
    public String getAuthor() {
        return this.author;
    }
    // More code for class Book
}

并在您的 displayBooksFromAuthor() 方法中:

public void displayBooksFromAuthor(String author) {
    for(Book b : bookList) {
        if(b.getAuthor().equals(author))
            System.out.println(b);
    }
}