从数组中打印出我的对象时出错

时间:2013-12-20 17:14:13

标签: java arrays

我在打印出我的数组中的详细信息时遇到了一些麻烦。当我在两本书的细节中运行我的addBook i时,但是当我从菜单中选择选项2时,我得到一个运行时错误(outofbounds), 通过将[i]添加到打印行并更改循环长度来解决上述问题。

我现在遇到的问题,如果我的借书来自我的借书,它没有递增。

import java.util.Scanner;

public class library {
    static Scanner keyboard = new Scanner(System.in);
    static boolean run = true;
    public static fiction [] fictionArray = new fiction[2];
    public static nonfiction [] nonfictionArray = new nonfiction[2];


    public static void main (String[] args){                             // main class method
        while (run){                    // this while statement allows the menu to come up again
            int answer = 0;             // answer initialized to Zero
            boolean isNumber;
            do{                              // start of validation
                System.out.println("1.  Add book");      // Menu selections
                System.out.println("2.  Display the books available for loan");
                System.out.println("3.  Display the books currently on loan");
                System.out.println("4.  Make a book loan");
                System.out.println("5.  Return book ");
                System.out.println("6   Write book details to file");
                if (keyboard.hasNextInt()){                       // I would like to set values to  =>1 <=6
                    answer = keyboard.nextInt();                  // this is more validation for the input for menu selection
                    isNumber = true;
                }   else {                                        // else if number not entered, it will prompt for the correct input
                    System.out.print(" You must enter a number from the menu to continue. \n");
                    isNumber = false;
                    keyboard.next();                                // clears keyboard

                }
            }
            while (!(isNumber));                                     // while to continue program after the do has completed
            switch (answer){                                         // switch statement - uses answer from the keyboard to select a case

                case 1:
                    addBook();                                    // adds book
                    break;
                case 2:
                    for (int i=0; i<5; i++){
                    if (fictionArray[i] != null){
                            System.out.println(fictionArray);}
                    if (nonfictionArray[i] != null){
                        System.out.println(nonfictionArray);}}

                     break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
            }
        }
    }
        static void addBook(){
            loanbook [] loanArray = new loanbook[2];
            String title,author;
            int choice;
            for(int x = 0; x < loanArray.length; x++){
            System.out.print("Press 1 for Fiction or 2 for Non Fiction: ");  // sub menu for fiction and non fiction
            choice = keyboard.nextInt();
            if (choice == 1){
                for(int count = 0; count < fictionArray.length; count++){
                    System.out.print("Enter title: ");
                    title= keyboard.nextLine();
                    title= keyboard.nextLine();
                    System.out.print("Enter author: ");
                    author= keyboard.nextLine();
                    fictionArray[count] = new fiction(title, author);
                    System.out.println("The book information you entered was :  " + fictionArray[count].toString()); // this will show the entry which was inout to the array
                    count++; }}
            else if (choice == 2) {
                for(int count = 0; count < nonfictionArray.length; count++){
                    System.out.print("Enter title: ");
                    title= keyboard.nextLine();
                    title= keyboard.nextLine();
                    System.out.print("Enter author: ");
                    author= keyboard.nextLine();
                    nonfictionArray[count] = new nonfiction(title, author);
                    System.out.println("The book information you entered was :  " + nonfictionArray[count].toString()); // this will show the entry which was inout to the array
                    count++;}}
            else{ int noBooks = loanArray.length;
                for (int i=0; i<noBooks; i++){
                    System.out.print(loanArray[x]);
                }}}} // addbook



} // Library end

下面是我的超类,然后是我的子类

public class loanbook {
    private String title,author;
    private int bookID;

    public loanbook(String pTitle,String pAuthor){
        bookID = 0;
        title = pTitle;
        author = pAuthor;
        bookID++;
    }  // Constructor
    public void setTitle(String pTitle){
        title = pTitle;
    } // setTitle
    protected String getTitle(){
        return title;
    }   // getTitle
    protected String getAuthor(){
        return author;
    }   // getAuthor
    public String toString(){
        return "\n BookID: "+ bookID+"\n" + " Title: "+ getTitle()+"\n" +" Author : "+ getAuthor()+ "\n";
    }

}  // loanbook

除了类名和构造函数

之外,我的子类是相同的
public class fiction extends loanbook {
    String bookType;
    private String getBookType;        // Would be fiction

    public fiction(String pTitle,String pAuthor){
        super(pTitle,pAuthor);
    }    // constructor
    protected void setBookType (String pBookType){
        bookType = pBookType;
    }  // setter for bookType

    protected String getBookType(){
        return "Fiction";
    }
    public String toString(){
        return super.toString() +" This book is : "+ getBookType();
    }
}   // class

2 个答案:

答案 0 :(得分:2)

您已宣布自己的fictionarraynonfictionarray长度为2.但是,在您的情况2中,您循环了5次:

for (int i=0; i<5; i++){
    if (fictionArray[i] != null){

将其更改为2。您可能在声明中更改了数组长度,但忘记更改循环迭代。在这种情况下,您可以使用数组的长度:

for (int i = 0; i < fictionArray.length; i++) {

此外,看起来您想要打印出特定的数组元素,而不是数组本身:

System.out.println(fictionArray[i]);

同样适用于nonfictionarraynonfiction类。

答案 1 :(得分:1)

我看到的两件事

if (fictionArray[i] != null){
     System.out.println(fictionArray);}         
if (nonfictionArray[i] != null){
     System.out.println(nonfictionArray);}}

您正在尝试打印整个数组System.out.println(fictionArray)。您可能需要System.out.println(fictionArray[i])

如果要循环5次

,还应将数组大小设置为5