找不到合适的方法(书籍)

时间:2015-02-03 20:10:45

标签: java arraylist

我正在displayBooksFromAuthor()工作,它将显示用户输入指定的作者。我试图这样做没有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);
        }
    }
}

2 个答案:

答案 0 :(得分:3)

错误位于以下方法的第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);
    }
}

答案 1 :(得分:3)

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

/*
   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);
    }
}

您已经从b对象中提取了一本书bookList,因此您需要做的就是使用b。另外,您无法将字符串与==进行比较;使用compareTo()equals()

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

或者,您可以使用:.equalsIgnoreCase().compareTo().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课程中,你应该写下这样的东西:

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);
    }
}