Súper y subclases

  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

Clase de aplicación:

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

preguntado el 02 de diciembre de 13 a las 19:12

Why instead of comment what you've done, post your code? -

its a very long code that has 4 different java files and its menu driven there is a lot of print statments etc -

Material relacionado: stackoverflow.com/questions/12878879/… Also try removing static from the declaration. -

Intenta hacer un SSCCE, then you isolate the problem and it will easier for you and for us if you still need help . -

1 Respuestas

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.

Respondido el 02 de diciembre de 13 a las 19:12

it is increased by one - user3057408

Yes, in your method, you set it to 0. Then at the end you increase it to 1. But, when you add your next book it starts at 0 again, and increases to 1 again. adding static to a method does not keep int i at the same value it was before. - Doble doble

How can I amend this ? - user3057408

If I have I outside of the method will that help - user3057408

Easiest way is to move i somewhere above the method where the method will have access to it. - Doble doble

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.