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

Java 从数组打印出我的对象时出错,java,arrays,Java,Arrays,我在打印我放入数组的细节时遇到了一些问题。当我运行addBook时,我得到了两本书的详细信息,但当我从菜单中选择选项2时,我得到了一个运行时错误(outofbounds), 通过将[i]添加到打印行并更改循环长度,可以解决上述问题 我现在遇到的问题是,如果我的BookID来自我的借书簿,它不是递增的 import java.util.Scanner; public class library { static Scanner keyboard = new Scanner(System.

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

我现在遇到的问题是,如果我的BookID来自我的借书簿,它不是递增的

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 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。但是,在您的情况下
2
,您将循环5次:

for (int i=0; i<5; i++){
    if (fictionArray[i] != null){
此外,您似乎希望打印出特定的数组元素,而不是数组本身:

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

同样,对于
非虚构数组
非虚构类

您已经声明了
虚构数组
非虚构数组
的长度为2。但是,在您的情况下
2
,您将循环5次:

for (int i=0; i<5; i++){
    if (fictionArray[i] != null){
此外,您似乎希望打印出特定的数组元素,而不是数组本身:

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

同样,对于
非虚构数组
非虚构类

您已经声明了
虚构数组
非虚构数组
的长度为2。但是,在您的情况下
2
,您将循环5次:

for (int i=0; i<5; i++){
    if (fictionArray[i] != null){
此外,您似乎希望打印出特定的数组元素,而不是数组本身:

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

同样,对于
非虚构数组
非虚构类

您已经声明了
虚构数组
非虚构数组
的长度为2。但是,在您的情况下
2
,您将循环5次:

for (int i=0; i<5; i++){
    if (fictionArray[i] != null){
此外,您似乎希望打印出特定的数组元素,而不是数组本身:

System.out.println(fictionArray[i]);
同样,对于
非小说类
非小说类
我看到了两件事

if (fictionArray[i] != null){
     System.out.println(fictionArray);}         
if (nonfictionArray[i] != null){
     System.out.println(nonfictionArray);}}
您正在尝试打印整个数组
System.out.println(虚构数组)
。您可能需要
System.out.println(虚构数组[i])

另外,如果要循环5次,则应将数组大小设置为5

if (fictionArray[i] != null){
     System.out.println(fictionArray);}         
if (nonfictionArray[i] != null){
     System.out.println(nonfictionArray);}}
您正在尝试打印整个数组
System.out.println(虚构数组)
。您可能需要
System.out.println(虚构数组[i])

另外,如果要循环5次,则应将数组大小设置为5

if (fictionArray[i] != null){
     System.out.println(fictionArray);}         
if (nonfictionArray[i] != null){
     System.out.println(nonfictionArray);}}
您正在尝试打印整个数组
System.out.println(虚构数组)
。您可能需要
System.out.println(虚构数组[i])

另外,如果要循环5次,则应将数组大小设置为5

if (fictionArray[i] != null){
     System.out.println(fictionArray);}         
if (nonfictionArray[i] != null){
     System.out.println(nonfictionArray);}}
您正在尝试打印整个数组
System.out.println(虚构数组)
。您可能需要
System.out.println(虚构数组[i])



此外,如果要循环5次,应将数组大小设置为5。请发布错误的完整堆栈跟踪。请注意,
virtualarray
的长度为2,但您可以将其迭代到5..还有一个建议:您通常不想将所有代码放在main方法中。请发布错误的完整堆栈跟踪。请注意,
virtualarray
的长度为2,但您可以将其迭代到5..还有一个建议:您通常不想将所有代码放在main方法中。请发布错误的完整堆栈跟踪。请注意,
virtualarray
的长度为2,但您可以将其迭代到5..还有一个建议:您通常不想将所有代码放在main方法中。请发布错误的完整堆栈跟踪。请注意,
virtualarray
的长度为2,但您可以将其迭代到5..还有一个建议:您通常不想将所有代码放在main方法中。谢谢,这解决了问题,但现在它只打印一本书。它也不会增加超级类的bookID,我想我已经回答了你前面的问题。我的回答是对图书ID的修正。你需要使它成为静态的。并在0处声明和初始化它<代码>int bookID=0将其从构造函数中取出。离开
bookID++
我无法告诉你为什么它只打印一本书。你的代码让我有点害怕:)。太多了。上面有两条评论。。应该是
public static int bookID=0嗨,我已经解决了bookID问题,非常感谢。我认为每次循环都会重写最后一个数组,这可能是因为循环从零开始。我已经修改了我的代码,我将发布一个新问题。谢谢,这解决了问题,但现在它只打印一本书。它也不会增加超级类的bookID,我想我已经回答了你前面的问题。我的回答是对图书ID的修正。你需要使它成为静态的。并在0处声明和初始化它<代码>int bookID=0将其从构造函数中取出。离开
bookID++
我无法告诉你为什么它只打印一本书。你的代码让我有点害怕:)。太多了。上面有两条评论。。应该是
public static int bookID=0嗨,我已经解决了bookID问题,非常感谢。我认为每次循环都会重写最后一个数组,这可能是因为循环从零开始。我已经修改了我的代码,我将发布一个新问题。谢谢,这解决了问题,但现在它只打印一本书。它也不会增加超级类的bookID,我想我已经回答了你前面的问题。我的回答是对图书ID的修正。你需要使它成为静态的。并在0处声明和初始化它<代码>int bookID=0将其从构造函数中取出。离开
bookID++
我无法告诉你为什么它只打印一本书。你的代码让我有点害怕:)。太多了。上面有两条评论。。应该是
public static int bookID=0嗨,我已经解决了bookID问题,非常感谢。我认为每次循环都会重写最后一个数组,它可能会