package asd; import java.util.*; public class Book { private String title; String[] authors; private int page; private int pubYear; public Book(String title, String[] authors, int page, int year) { setTitle(title); setAuthors(authors); setPage(page); setYear(year); } public void setTitle(String title) { this.title = title; } public String getTitle() { return title; } public void setAuthors(String[] authors) { this.authors = authors; } public String getAuthors() { return Arrays.toString(authors); } public void setPage(int page) { this.page = page; } public int getPage() { return page; } public void setYear(int pubYear) { this.pubYear = pubYear; } public int getYear() { return pubYear; } public String toString() { return title + " - " + Arrays.toString(authors) + " - " + page + " pages - " + pubYear + "."; } }

package asd; public class Bookshelf { int bookCapacity = 0; int bookHave = 0; Book[] books; Book[] result; Bookshelf(Book[] book) { books = new Book[book.length]; bookCapacity = book.length; result = new Book[bookCapacity]; for (int i = 0; i < book.length; i++) { this.books[i] = book[i]; } } void searchByTitle(String keyword) { for (int i = 0; i < bookCapacity; i++) { if (books[i].getTitle().contains(keyword)) { result[bookHave] = books[i]; bookHave++; } } System.out.println("Found " + bookHave + " book(s)."); for (int i = 0; i < bookHave; i++) { System.out.println(i + 1 + ". " + result[i]); } } void searchByAuthor(String keyword) { for (int i = 0; i < bookCapacity; i++) { if (books[i].getAuthors().contains(keyword)) { result[bookHave] = books[i]; bookHave++; } } System.out.println("Found " + bookHave + " book(s)."); for (int i = 0; i < bookHave; i++) { System.out.println(i + 1 + ". " + result[i]); } } void searchByBoth(String keyword) { for (int i = 0; i < bookCapacity; i++) { if (books[i].getAuthors().contains(keyword) || books[i].getTitle().contains(keyword)) { result[bookHave] = books[i]; bookHave++; } } System.out.println("Found " + bookHave + " book(s)."); for (int i = 0; i < bookHave; i++) { System.out.println(i + 1 + ". " + result[i]); } } }



package asd; import java.util.Scanner; public class Test { public static void main(String[] args) { Book[] books = new Book[3]; books[0] = new Book(" Harry Potter And The Sorcerer's Stone", new String[] { "J.K.Rowling" }, 428, 1998); books[1] = new Book("Harry Potter And the Goblet Of Fire", new String[] { "J.K.Rowling", "Mary GrandePre" }, 752, 2002); books[2] = new Book(" The Casual Vacancy", new String[] { "J.K.Rowling", "Kim", "Mary GrandePre" }, 513, 2012); Bookshelf shelf = new Bookshelf(books); String keyword; Scanner input = new Scanner(System.in); int choice; while(true){ System.out.println("1. Search by title."); System.out.println("2. Search by author."); System.out.println("3. Search by both."); System.out.print("User input: "); choice = input.nextInt(); input.nextLine(); if (choice == 1) { System.out.print("Insert title keyword: "); keyword = input.nextLine(); shelf.searchByTitle(keyword); System.out.println("-----------------------------------------------------------"); } if (choice == 2) { System.out.print("Insert author keyword: "); keyword = input.nextLine(); shelf.searchByAuthor(keyword); System.out.println("-----------------------------------------------------------"); } if (choice == 3) { System.out.print("Insert title or author keyword: "); keyword = input.nextLine(); shelf.searchByBoth(keyword); System.out.println("-----------------------------------------------------------"); } } } }


길어서 읽을 사람도 없겠지만


일단 실행하면


첫번째 실행은 잘되는데


두번째 실행은


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at asd.Bookshelf.searchByTitle(Bookshelf.java:23) at asd.Test.main(Test.java:36)


뭐 배열크기를 넘어갔다는것같은데 어떻게 수정해야하지