4

Preface: I'm terrible with java and just learning. Searched a ton, couldn't find an answer. We CANNOT use arraylists.

I have a Book class that contains info in a specific book like pages, author, etc.

I have a Bookshelf class that reads in an array of Book objects. I need to write a method that takes in an author name, finds all the books in the Book array that are by that author, then return an array of those books.

My plan is to find the total number of books by that author and store it in a variable. Then create a new array of that size. I just don't know how to take select elements from the Book array and put them into the new array.

What I have so far, not sure if it's correct...

public Book[] getBooksByAuthor(String author) {
    int count = 0;
    String a  = author;

    for(int i = 0; i < books.length; i++){
        if(books[i].getAuthor().equals(a)){
            count += 1;
        }
    }
4
  • looks good so far, now you need to create a new array and iterate again to add the elements to the new array Commented Oct 8, 2017 at 20:10
  • 2
    That's correct so far. Do you know how to create the array using the count variable? Commented Oct 8, 2017 at 20:10
  • Book[] newBooks = new Book[count]; will make a new array of size count. but if I loop through again and assign values to the new array, i could be creating some empty indices because there was no match, if that makes sense. Commented Oct 8, 2017 at 20:20
  • Please do not edit your question with new code as you find better ways, because that changes the question. Post an answer instead if you find a solution yourself. Commented Oct 8, 2017 at 20:39

3 Answers 3

4

Java 8's streams give you a pretty elegant way of doing this:

public Book[] getBooksByAuthor(String author) {
    return Arrays.stream(books)
                 .filter(b -> b.getAuthor().equals(author))
                 .toArray(Book[]::new);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I would say just "Java", not "Java 8", because Java 8 is quite old now and can be assumed to be the version used by everyone now.
@Bohemian not true. For example my school (which is a good engineer school here in Switzerland) still uses java 7...
@paul if your school is still using a version of Java that was released 6 years ago and was superseded 2.5 years ago, then it's not a good school. Very few employers would still be on version 7, so your "good" school is teaching more or less unemployable skills.
@Bohemian well java is just a course we get in second year in computer science (the school is really big so there are a lot of different paths) so I think they don't really care as we mostly do C, but I think you are right I will ask the direction if we can get this changed. Thanks for opening my mind man
1

My final result:

public Book[] getBooksByAuthor(String author) {
    int count = 0;
    String a  = author;
    int iter = 0;

    for(int i = 0; i < books.length; i++){
        if(books[i].getAuthor().equals(a)){
            count += 1;
        }
    }
    Book[] newBooks = new Book[count];
    for (int i = 0; i < books.length; i++){
        if(books[i].getAuthor().equals(a)){
            newBooks[iter] = books[i];
            iter += 1;

        }
    }
    return newBooks;
}

Comments

0

If you can not use Arrays, you could do it this way :

String booksFromAuthor = "";
for(final String book : books){
    if(book.equals(author))
        booksFromAuthor += book + "|";
}
return booksFromAuthor.split("|");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.