First, I feel it would be beneficial to define my goal. The goal is to take my titles array, assign it the title values that are defined in my bookList array then sort the titles array. Up to there I think I've done fine, that all seems to work.

The part that I am having trouble with is bouncing the sort back to the bookList array. I have the feeling I have to make a new array to do this, but I am unsure of how to take the string value from the bookList array and then compare it to the titles array.

Here's the code I have thus far (there's another class that has all the get and set methods for everything, so the method gettitle does work, but my problem isn't in that part of the code so I only provided this, hopefully that's enough):

Removed original code to show the new code.

Later I will have to add up the price of each book, but I think I know how to do that using a loop. As always, I will be working on it too, so if I figure it all out, I will update the thread.

Many thanks, and apologies for being such a novice.


public class BookStore2 {
    public static void main(String[] args) {
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        Book[] bookList = new Book [5];
        bookList[0] = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f);
        bookList[1] = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
        bookList[2] = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
        bookList[3] = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f);
        bookList[4] = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f);
        String[] titles = new String [5];
        for (int i = 0; i < bookList.length; i++){
            titles[i] = bookList[i].gettitle();
        Book[] sortedBooks = new Book [5];
        for (int j = 0; j < 5; j++){
            for (int k = 0; k < 5; j++){
                if (titles[j].equals(bookList[k].gettitle())){
                    sortedBooks[j] = bookList[k];
                } // end if 
            } //end For 2
        } // end For 1
        } // end Main Method
    } // end BookStore2 class

While it seems to have proper syntax it provides an index out of bounds error. I'm thinking this is what we are supposed to do for this, but it seems like a very odd way to go about it. Am I doing this completely wrong or am I just making a small misstep somewhere?