2016-11-27 56 views
-2

我正在研究一种方法(public Book[] getBooksWrittenBy(String authorLastName)),该方法应按作者的姓氏返回书籍对象数组。我的老师通过电子邮件将其编码为正确的:检查所有书籍是否在构造函数中,并且在构造函数中有20本书,但是当我尝试在BlueJ项目中运行测试任务课程时,它说java.lang.ArrayIndexOutOfBoundsException: 0。当我测试一个对象时,它总是显示作者的0本书。任何人都可以看到为什么这种方法会有问题?它是以下课程中的最后一种方法:Book []:数组索引超出范围

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