Hi guys,
I'm writing a program to do several things involving getting information from the user about a book (title, author, publisher etc.)

There is a Book class and then the client class BookClient.
I got the program working for the user entering a single book but then I changed it so the user could enter an array of books.
I can enter in new books fine but when it comes to outputting, nothing happens.
It works when I have the loop for entering the books and the loop for outputting the books in the same method but not in different ones.

Any help would be greatly appreciated.
Thanks in advance :)

Here is the Book class:

import java.util.*;

public class Book
{
    private String title;
    private String author;
    private String publisher;
    private int year;
    private long isbn;
    private double price;
    private int quantity;

    public void readInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Title: ");
        title = keyboard.nextLine();

        System.out.print("Author: ");
        author = keyboard.nextLine();

        System.out.print("Publisher: ");
        publisher = keyboard.nextLine();

        System.out.print("Year: ");
        year = keyboard.nextInt();

        System.out.print("ISBN: ");
        isbn = keyboard.nextLong();

        System.out.print("Price: ");
        price = keyboard.nextDouble();

        System.out.print("Quantity: ");
        quantity = keyboard.nextInt();
    }

    public void writeOutput()
    {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Publisher: " + author);
        System.out.println("Year: " + year);
        System.out.println("ISBN: " + isbn);
        System.out.println("Price: " + price);
        System.out.println("Quantity: " + quantity);
    }

    public double value()
    {
        return quantity * price;
    }

    public void setBook(String newTitle, String newAuthor, String newPublisher, int newYear, long newIsbn, double newPrice, int newQuantity)
    {
        title = newTitle;
        author = newAuthor;
        publisher = newPublisher;
        
        if(newYear < 0)
        {
            System.out.println("Error: negative year.");
            System.exit(0);
        }

        else
            year = newYear;

        if(newIsbn < 0)
        {
            System.out.println("Error: negative ISBN.");
            System.exit(0);
        }

        else
            isbn = newIsbn;

        if(newPrice < 0)
        {
            System.out.println("Error: negative price.");
            System.exit(0);
        }

        else
            price = newPrice;

        if(newQuantity < 0)
        {
            System.out.println("Error: negative year.");
            System.exit(0);
        }

        else
            quantity = newQuantity;
    }

    public String getTitle()
    {
        return title;
    }

    public String getAuthor()
    {
        return author;
    }

    public String getPublisher()
    {
        return publisher;
    }

    public int getYear()
    {
        return year;
    }

    public long getIsbn()
    {
        return isbn;
    }

    public double getPrice()
    {
        return price;
    }
    
    public int getQuantity()
    {
        return quantity;
    }
}

and the client class:

import java.util.*;
    
public class BookClient
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        
        int num = 0;
        Book[] bookInfo = new Book[num];

        Choice1(bookInfo);
        Choice2(bookInfo);

    }

    public static Book[] Choice1(Book[] bookInfo)    
    {
        System.out.print("Number of books to enter: ");
        Scanner keyboard = new Scanner(System.in);
        int num = keyboard.nextInt();

        bookInfo = new Book[num];

        for(int i = 0; i < bookInfo.length; i++)
        {
            System.out.println();
            bookInfo[i] = new Book();
            bookInfo[i].readInput();
            System.out.println();
        }

        return bookInfo;
    }

    public static void Choice2(Book[] bookInfo)
    {
        for(int i = 0; i < bookInfo.length; i++)
            bookInfo[i].writeOutput();
    }
}

You can try adding a few print messages between your calls in order to see that happens. For example:

int num = 0;
Book[] bookInfo = new Book[num];
 
System.out.println("Length before Choice1: "+bookInfo.length);
Choice1(bookInfo);
System.out.println("Length after Choice1: "+bookInfo.length);

Choice2(bookInfo);

There also is a better design for your problem:

Book[] bookInfo = Choice1();
Choice2(bookInfo);

public static Book[] Choice1()    
    {
        System.out.print("Number of books to enter: ");
        Scanner keyboard = new Scanner(System.in);
        int num = keyboard.nextInt();

        Book [] bookInfo = new Book[num];

        for(int i = 0; i < bookInfo.length; i++)
        {
            System.out.println();
            bookInfo[i] = new Book();
            bookInfo[i].readInput();
            System.out.println();
        }

        return bookInfo;
    }

Either change the "Choice1" method to void and remove lines 23 and 33 (above), or change "Choice1" so that it does not take any argument, change line 23 (above) to include the Type declaration, and "combine" lines 10 and 12 (above).

All the line numbers in this post refer to the client class.

By redefining the passed in array in "Choice1", you lose the reference to the original method that was passed in, and by not reassigning the result of the method back to the original bookInfo reference when calling Choice1, you are left with an "empty" array.

This article has been dead for over six months. Start a new discussion instead.