2016-11-27 8 views
-2

私は著者の姓でブックオブジェクトの配列を返すことになっているメソッド(public Book[] getBooksWrittenBy(String authorLastName))に取り組んでいます。すべての書籍がコンストラクタに含まれているかどうかを確認し、コンストラクタに20冊すべての書籍がありますが、BlueJプロジェクトでテスト割り当てクラスを試してみるとjava.lang.ArrayIndexOutOfBoundsException: 0と表示されます。私がオブジェクトをテストすると、著者は常に0冊の本を表示します。なぜその方法に問題があるのか​​誰にでも見えますか?Book []:Array Index of Bounds

public class BookStore 
{ 
    private String storeName; //e.g. "Jason's New Books" 
    private Book[] inventory; //Array of Book objects 

/** 
* creates the inventory Array of 100 book object 
* note that the list does not contain birth years nor death years for the authors, nor 
* years published for the books...just use 2013 for all of these dates 
* booklist[0] = new Book(new Author)(new Name("first", "last", "middle"), new Date(), new Date(), 
* new Date(), "ULYSSES") 
*/ 

public BookStore() 
{ 

    inventory = new Book[20]; 
    Book b1 = new Book(new Author(new Name("James", "Joyce", " "), new Date(2013,1,1), new Date()), 
      new Date(), "ULYSSES"); 
    inventory[0] = b1;    

    inventory[1] = new Book(new Author(new Name("F.", "Fitzgerald", "Scott"), new Date(2013,1,1), new Date()), 
     new Date(), "The Great Gatsby"); 
    inventory[2] = new Book(new Author(new Name("James", "Joyce", " "), new Date(2013,1,1), new Date()), 
     new Date(), "A Portrait Of The Artist As A Young Man"); 
    inventory[3] = new Book(new Author(new Name("Vladimir", "Nabokov", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Lolita"); 
    inventory[4] = new Book(new Author(new Name("Aldous", "Huxley", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Brave New World"); 
    inventory[5] = new Book(new Author(new Name("William", "Faulkner", " "), new Date(2013,1,1), new Date()), 
     new Date(), "The Sound and the Fury"); 
    inventory[6] = new Book(new Author(new Name("Joseph", "Heller", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Catch-22"); 
    inventory[7] = new Book(new Author(new Name("Arthur", "Koestler", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Darkness at Noon"); 
    inventory[8] = new Book(new Author(new Name("D.", "Lawrence", "H."), new Date(2013,1,1), new Date()), 
     new Date(), "Sons and Lovers"); 
    inventory[9] = new Book(new Author(new Name("John", "Steinbeck", " "), new Date(2013,1,1), new Date()), 
     new Date(), "The Grapes of Wrath"); 
    inventory[10] = new Book(new Author(new Name("Malcolm", "Lowry", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Under the Volcano"); 
    inventory[11] = new Book(new Author(new Name("Samuel", "Butler", " "), new Date(2013,1,1), new Date()), 
     new Date(), "The Way of all Flesh"); 
    inventory[12] = new Book(new Author(new Name("George", "Orwell", " "), new Date(2013,1,1), new Date()), 
     new Date(), "1984"); 
    inventory[13] = new Book(new Author(new Name("Robert", "Graves", " "), new Date(2013,1,1), new Date()), 
     new Date(), "I, Claudius"); 
    inventory[14] = new Book(new Author(new Name("Virginia", "Woolf", " "), new Date(2013,1,1), new Date()), 
     new Date(), "To the Lighthouse"); 
    inventory[15] = new Book(new Author(new Name("Theodore", "Dreiser", " "), new Date(2013,1,1), new Date()), 
     new Date(), "An American Tragedy"); 
    inventory[16] = new Book(new Author(new Name("Carson", "McCullers", " "), new Date(2013,1,1), new Date()), 
     new Date(), "The Heart is A Lonely Hunter"); 
    inventory[17] = new Book(new Author(new Name("Kurt", "Vonnegut", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Slaughterhouse-Five"); 
    inventory[18] = new Book(new Author(new Name("George", "Orwell", " "), new Date(2013,1,1), new Date()), 
     new Date(), "Animal Farm"); 
    inventory[19] = new Book(new Author(new Name("W.", "Maugham", "Somerset"), new Date(2013,1,1), new Date()), 
     new Date(), "Of Human Bondage");    
} 

/** 
* creates the inventory Array of 100 Book object 
* note that the list does not contain birth years nor death years for the authors, nor 
* years published for the books...just use 2013 for all of these dates 
* stores the storeName parameter in the storeName instance variable, but only if it 
* does not equal "Taylor's Used Books". If the parameter is "Taylor's Used Books", 
* store the name as "Jason's Used Books" instead. 
* @param storeName - the name of the book store 
*/ 

public BookStore(String storeName) 
{ 
    if(storeName.equals("Taylor's Used Books")) 
    { 
     this.storeName = storeName; 
    }else if(storeName.equals("Taylor's Used Books")) 
    { 
     storeName = "Jason's Used Books"; 
    }else 
    { 
     this.storeName = storeName; 
    } 


} 

/** 
* accessor that gets store name 
* @return store name 
*/ 

public String getStoreName() 
{ 
    return storeName; 
} 

/** 
* mutator that sets store name 
* @param - store name 
*/ 

public void setStoreName(String storeName) 
{ 
    this.storeName = storeName; 
} 

/** 
* returns the number of books written by an author whose last name is authorLastName 
* use a for loop 
* use .equalsIgnoreCase() 
*/ 

public int howManyBooksDidThisAuthorWrite(String authorLastName) 
{ 
    int counter = 0; 

    for(int i = 0; i < inventory.length; i++) 
    { 
     if(inventory[i].getAuthorName().equalsIgnoreCase(authorLastName)) 
     { 
      counter++; 
     } 
    } 
    return counter; 
} 

/** 
* returns the full name of the author who wrote the book by this title 
* returns null if there is no Book with this title, or if title is null of "" 
* use a for loop 
*/ 

public String getAuthorFullName(String title) 
{ 
    for(Book authorName : inventory) 
     if (authorName.getTitle() != null && authorName.getTitle().equalsIgnoreCase(title)) 
     { 
      return authorName.getAuthor().getName().getFullName(); 
     } 
    return null; 
} 

/** 
* returns null if there is no Book written by an author with authorLastName as their 
* last name 
* otherwise, returns an Array of Book objects(hint: declare and return a local Book[] 
* object containing all Book objects which were written by an author with this last name 
* (e.g. Orwell has two books in the list: 1984 and ANIMAL FARM) 
*/ 

public Book[] getBooksWrittenBy(String authorLastName) 
{ 
    int byAuthor = 0; 
    for(int i = 0; i < inventory.length; i++) 
    { 
     if(inventory[i].getTitle().equals(authorLastName)) 
     { 
      byAuthor++;  
     } 
    } 

    Book[] matches = new Book[byAuthor]; 
    int indexNewArray = 0; 
    for(int j=0; j < inventory.length; j++){ 
     if(inventory[j].getTitle().equals(authorLastName)) 
     { 
      matches[indexNewArray] = inventory[j]; 
      indexNewArray++; 
     } 

    } 
    return matches; 
} 
+0

これは非常に多くのコードです。 –

答えて

1

完全な答えを出すことは不可能です。

は、しかし常識から、私は何かがここにあなたのロジックと間違っている可能性が疑われる:

inventory[i].getTitle().equals(authorLastName)

これが問題である理由あなたが見ることができますか? (ヒント:コードラインが英語で何を言っているのか、具体的にはgetTitle()とは何か、それともauthorLastNameを意味するのかを考えてください)

+0

気にしない、私はちょうどラインを変更することによって解決した:inventory [i] .getAuthor()。getName()。getLastName()。equals(authorLastName))。それは私のプロジェクト全体をカスケードし、すべてを修正しました。私はそれを見たい人のためにこの投稿を残します。 – NCP