试图从arraylist打印方法

时间:2015-01-15 23:50:55

标签: java

我正在尝试使用我的库类中的数组从我的成员类中打印标题  公共类Book给出了这个错误: http://screencast.com/t/tqpJp2BF8sH

       {
    // instance variables - replace the example below with your own
    private int x;
    private Integer bookid;
    private String author;
    private String title;
    private String ficornonfic;

    /**
     * Constructor for objects of class Book
     */
    public Book(Integer bookID, String Author, String Title, String FictionORnonfiction  )
    {
        bookid = bookID;
        author = Author;
        title = Title  ; 
        ficornonfic = FictionORnonfiction;
        x = 0;
    }


    public String PrintListOfBooks()
  {

        return title;

    }

    public String toString() {
     return "Title:" + title + " BookId: " + bookid  + " Author: " + author + ".";
   }
   public int getTitle() {
     return this.title;
}
}
                ---------------------------------------------------------

这是我的图书馆课

   import java.util.ArrayList;



    public class Library
    {


        private ArrayList<Member>listOfMembers;
        public Library()
        {
            listOfMembers = new ArrayList<Member>();
             listOfBooks = new ArrayList<Book>();
        }
        public void storeMember(Member Member)
        {
            listOfMembers.add(Member);
        }
        public int numberOfMembers()
        {
            return listOfMembers.size();
        }

        public void listMembers()
       {
       for (int item=0; item<listOfMembers.size(); item++ ) {
         Member m = listOfMembers.get (item);
        System.out.println(m.GetWholeName());
            }
        } 

    public Member findMember(int id) {

         for(Member member : listOfMembers) {
             if (member.getId() == id) {
                  return member;
             }
         }

         return null;
    }

    private ArrayList<Book>listOfBooks;

        public void storeBook(Book Book)
        {
            listOfBooks.add(Book);
        }
        public int numberOfBooks()
        {
            return listOfBooks.size();
        }

        public void listBooks()
       {
       for (int item=0; item<listOfBooks.size(); item++ ) {
         Book b = listOfBooks.get (item);
        System.out.println(b.PrintListOfBooks());
            }
        }

        public Book findBook(string title) {

         for(Book book : listOfBooks) {
             if (book.getId() == id) {
                  return book;
             }
         }

         return null;
    }


    }

我正在尝试使用我的库类中的数组从我的成员类中打印标题  公共类Book给出了这个错误: http://screencast.com/t/tqpJp2BF8sH

3 个答案:

答案 0 :(得分:2)

titleString,而不是int。您可以更改方法签名以匹配字段类型:

public String getTitle() {
    return this.title;
}

答案 1 :(得分:0)

首先,正如其他人已经注意到您的titleString而不是int。因此,更改getTitle()的返回类型(或完全删除它)。因为您似乎已在toString()中正确覆盖Book以获取所有信息,并且您可以使用for-each loop来迭代List的内容。像,

for (Book b : listOfBooks) {
    System.out.println(b);
}

相当于

for (int i = 0; i < listOfBooks.size(); i++) {
    Book b = listOfBooks.get(i);
    System.out.println(b.toString());
}

最后在Java中,按照惯例,方法名称以小写字母开头。

答案 2 :(得分:0)

当你想要返回的值是String时,你实际上是告诉程序返回一个Integer。

如果将int更改为String,则错误将消失。

public String getTitle() {
return this.title;

}