-2

I have a huge problem with my code:

public class BookStore
{
    private ArrayList<Book> books;
}



/**
* This method takes the author's name as a String parameter and returns an
* arraylist of all the books written by that author. It uses a while loop
* and an iterator, locates the books written by that author (case-insensitive)
* and adds them to another arraylist. 
*/
public ArrayList<Book> getBooksByAuthor(String authorName){             
    ArrayList<Book> getBooksByAuthor =  new ArrayList<Book>();
    Iterator<Book> aBook = books.iterator();  
    while(aBook.hasNext()){
        Book aBookd = aBook.next();
        if (authorName.equalsIgnoreCase(aBookd.getAuthor())){  
            books.add(getAuthor());     
            books.addAll(getBooksByAuthor);
        }   
    } 
    return getBooksByAuthor.size();
}

Those 3 lines

  • books.add(getAuthor());
  • books.addAll(getBooksByAuthor); and the
  • return getBooksByAuthor.size();

I'm pretty sure that they are completely wrong. I tried different ways to do it ,but it didn't work. I really don't understand how to do that. Could someone help me?. Thank you for your time!

splrs
  • 2,424
  • 2
  • 19
  • 29
  • Have you tried running this code? It starts with a class that only defines an inaccessible-to-the-outside arraylist, without any methods to manipulate that list, and a single function that lives completely detached from the class. Where is the rest of your code that actually calls this function and does something? – Mike 'Pomax' Kamermans Nov 25 '14 at 00:07
  • It looks like you just want to do `getBooksByAuthor.add(aBookd);` in place of the first two lines you mention. And this code won't compile as is - `getBooksByAuthor.size();` is an `int`, you have to return an `ArrayList` - presumably `getBooksByAuthor`. Unrelatedly, your naming is atrocious. – drew moore Nov 25 '14 at 00:07

2 Answers2

0

I'm fairly certain you wanted to add the book(s) with matching author's name to a new List. Something with an implicit iterator using a for-each loop

List<Book> al = new ArrayList<>();
for (Book book : books) {
    if (authorName.equalsIgnoreCase(book.getAuthor())) {  
        al.add(book);     
    }   
} 
return al;

or using an explicit Iterator like

List<Book> al = new ArrayList<>();
Iterator<Book> iter = books.iterator();
while (iter.hasNext()) {
    Book book = iter.next();
    if (authorName.equalsIgnoreCase(book.getAuthor())) {
        al.add(book);
    }
}
return al;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

is there any specific need for the iterator and a while-loop instead of a foreach loop?

what (i think) you'd like to achive is in normal language is: we have an empty collection/list as result. for each book in the list of books, check if the author has an equal name to the given name - if the names are equal, we add the book to the resulting collection/list.

that in code looks like:

public ArrayList<String> getBooksByAuthor(String authorName) {
    ArrayList<Book> result = new ArrayList<Book>();
    for (Book aBook : books) { //[for each notation in java ][1]
        if (authorName.equals(aBook.getAuthor())) {
            result.add(aBook);
        }
    }
    return result;
}

if you'd like to use a while loop, read up the foreach/while loop conversion in the this link.

in addition and as mentioned in the comments, your code has some semantic and syntactic errors:

  • your return type is wrong (int instead of ArrayList)
  • your class definition closing brace ends before your method definition
  • you add the author object (probably a string) to your books-collection
  • you never add any book to your resulting collection
  • you try to addAll objects of your (empty) collection getBooksByAuthor to your books, instead of adding some/single books to your getBooksByAuthor collection

    [1] http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Community
  • 1
  • 1
Christian R.
  • 1,528
  • 9
  • 16