0

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.

The problem i am having now if my BookID from my loanbook, its not incrementing.

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
2
  • Please post a full stacktrace of your error. Note that fictionarray has a length of 2 but you iterate it up to 5.. Commented Dec 20, 2013 at 17:15
  • And just a suggestion: you generally don't want to put all your code in main method Commented Dec 20, 2013 at 17:16

2 Answers 2

2

You've declared your fictionarray and nonfictionarray to be of length 2. However, in your case 2, you are looping 5 times:

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

Change it to 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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). You probably want System.out.println(fictionArray[i])

Also you should set your array sizes to 5 if you want to loop 5 times

5 Comments

Thanks, this resolved the problem, but now it only prints one book. it also does not increment the bookID from the superclass
I think I has an answer to your previous question. I answered with the fix to the book ID. You need to make it static. And declare and initialize it at 0. int bookID = 0; Take it out of the constructor. Leave the bookID++
I couldn't tell you why it only prints one book. You code kinda scares me :). Too much going on.
Two comments above.. should be public static int bookID = 0;
Hi, i have resolved the bookID, many thanks. I think it is over writing the last array every i do my loop, it could be because the loop starts at zero. i have modified my code and i will post a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.