java.lang.Iterable error - What does this mean and how do I fix it?

泪湿孤枕 提交于 2020-01-25 12:08:12

问题


I've been trying to find an answer to this for a couple days now and have had no luck. It is very late and I am very tired so I thought I would put it up here and hope that someone out there can help. Here is my snippit of code that is popping an error in Eclipse.

private void filterByTitle() {
    String title = Validator.getLine(sc, "Enter the Title to retrieve: ");
    System.out.println("\n" + Book.getHeadings());
    for(Book book : bookList.filterListByTitle(title)) {
        System.out.println(book);
    }   
}

The part with the error is " : bookList.filterListByTitle(title))". The error is "Can only iterate over an array or an instance of java.lang.Iterable".

I am new to Java, so please don't just repeat the error. I know there is an error, but I don't know what it means or how to fix it. Please help if you can.

Thank you.


回答1:


bookList.filterListByTitle() needs to return an object that implements java.lang.Iterable or it needs to be an array. In that case, whatever it returns should implement the necessary method for Iterable or extend one of the abstract subclasses like AbstractList, OR be an array.




回答2:


This is because bookList.filterListByTitle method does not returns an array or iterable.

if its a single element then use "if clause" instead of "for" to check.



来源:https://stackoverflow.com/questions/20171235/java-lang-iterable-error-what-does-this-mean-and-how-do-i-fix-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!