Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top