简体   繁体   English

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

[英]Error when printing out my object from array

Im having some trouble printing out details ive put into my array. 我在打印输出到我的阵列中的细节时遇到了麻烦。 when i run my addBook i out in details of two books, but when i select option 2 from menu, i get a runtime error (outofbounds), Above is resolved by adding [i] to the printline and changing my loop length. 当我运行addBook时,我会详细介绍两本书,但是当我从菜单中选择选项2时,会出现运行时错误(越界),可以通过在打印行中添加[i]并更改循环长度来解决上述问题。

The problem i am having now if my BookID from my loanbook, its not incrementing. 我现在遇到的问题是,如果我的借书本中的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

Below is my Superclass , then my subclass 以下是我的超类,然后是我的子类

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

My subclasses are the same except for the class name and constructor 我的子类相同,除了类名和构造函数

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

You've declared your fictionarray and nonfictionarray to be of length 2. However, in your case 2 , you are looping 5 times: 您已经声明了fictionarraynonfictionarray的长度为2。但是,在您的情况2 ,您循环了5次:

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

Change it to 2 . 将其更改为2 It's possible you changed the array length in the declaration, but forgot to change the loop iteration. 您可能在声明中更改了数组长度,但是却忘记了更改循环迭代。 In that case, you can just use the array's length: 在这种情况下,您可以只使用数组的长度:

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

Additionally, it looks like you want to print out the specific array element, not the array itself: 此外,您似乎要打印出特定的数组元素,而不是数组本身:

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

and likewise for nonfictionarray and the nonfiction class. 同样适用于nonfictionarraynonfiction类。

Two things I see 我看到两件事

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

You're trying to print the entire array System.out.println(fictionArray) . 您正在尝试打印整个数组System.out.println(fictionArray) You probably want System.out.println(fictionArray[i]) 您可能需要System.out.println(fictionArray[i])

Also you should set your array sizes to 5 if you want to loop 5 times 另外,如果要循环5次,则应将数组大小设置为5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印出一组对象 - printing out an array of object 将对象添加到对象数组,然后从数组中打印出对象 - Adding objects to an object array and then printing out the objects from the array 为什么我的程序在打印出我的 letterList 数组时会跳过数字? 它只是打印出 AZ 和 az - Why does my program skip over digits when printing out my letterList array? It is only printing out A-Z and a-z 我的数组仅打印出文件中的最后一个元素,我希望它打印出所有 - My Array is only printing out last element from file, I want it to print out all 为什么从我的棋盘 class 创建的第二个 object 没有像我的第一个 object 一样打印出充满战舰的游戏棋盘? - Why is my second object created from my Board class not printing out the same game board filled with battleships like my first object? 从Java中的数组打印值时出现异常错误 - Exception error when printing values from an array in Java 为什么从arraylist进行打印时,数组超出范围? - why do i get an array out of bounds array when printing from arraylist? 在我的LinkedList中打印出一个元素时出现问题 - Issues printing out an element from my LinkedList 从数组中打印质数 - printing out prime numbers from array 从阵列打印时发生异常 - Exception when printing from an Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM