0
  1. this is my application class where i have instead array of objects

  2. loanBook is a super class and loanDocumentry is a sub class that extends loanBook. This is declared at the top of the application class

    public static loanBook[] bookArray = new loanDocumetry[5];

then in my application class i have to add new documentry book so i use the scanner for inputs and then use them to add the new object

bookArray[i] = new loanDocumentry(
    title, author, publisher, year, noOfPages, genre);

and the book count in loanBook increases so I know each time I run the method it creates new book but when then printing the array out it looks like it never added any of those books to the array and that the only one i have added is the last one

Application Class:

public class ApplicationClass {

public static Scanner input = new Scanner(System.in);
public static loanBook[] bookArray = new loanDocumentry[5];

public static void main(String[] args) {
    addBook();
}

public static void addBook() {
    input.nextLine();

    String title;
    String author;
    String publisher;
    int year;
    int noOfPages;
    String genre;
    String choice;
    int i = 0;


            System.out.print("\nTITLE of the book: ");
            title = input.nextLine();
            System.out.print("AUTHOR of the book: ");
            author = input.nextLine();
            System.out.print("PUBLISHER of the book: ");
            publisher = input.nextLine();
            System.out.print("YEAR book was published in: ");
            year = input.nextInt();
            System.out.print("NUMBER OF PAGES the book has: ");
            noOfPages = input.nextInt();
            System.out.print("GENRE of the book: ");
            input.nextLine();
            genre = input.nextLine();
            bookArray[i] = new loanDocuemntry(title, author, publisher, year, noOfPages, genre);
            i++;

}

loanBook Superclass & loanDocumentry Sub class both use set and gets

Ullas
  • 11,450
  • 4
  • 33
  • 50
  • 3
    Why instead of comment what you've done, post your code? – nachokk Dec 02 '13 at 19:45
  • its a very long code that has 4 different java files and its menu driven there is a lot of print statments etc – user3057408 Dec 02 '13 at 19:47
  • Related material: http://stackoverflow.com/questions/12878879/subtype-polymorphism-and-arrays Also try removing static from the declaration. – ryanlutgen Dec 02 '13 at 19:47
  • Try to make a [SSCCE](http://sscce.org), then you isolate the problem and it will easier for you and for us if you still need help . – nachokk Dec 02 '13 at 19:57

1 Answers1

0

Try doing a print on i from the line above bookArray[i] = new loanDocumenry( It should be changing while each new loanBook is added.

When you add a loanBook, i should be increased by 1 so you are setting bookArray[0], bookArray[1], bookArray[2], bookArray[3], bookArray[4]. You are only setting the first one.

DoubleDouble
  • 1,493
  • 10
  • 25