0

Im trying to create a new instance in my book array, i've researched on the copyof method and thats supposedly how you do it, however, when I run the code that supposedly does it, i dont get any error or anything but it just doesnt show up? the output shows the other 3 books but not the added harry potter one.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {

        Book book1 = new Book("The Alchemist", "Paulo Coelho", "HarperCollins", "Adventure");
        Book book2 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner\'s Sons", "Fiction");
        Book book3 = new Book("The Catcher in the Rye", "J. D. Salinger", "Little, Brown and Company", "Fiction");

// Create an instance of the class Author

        Author author1 = new Author("Paulo", "Coelho");
        Author author2 = new Author("F. Scott", "Fitzgerald");
        Author author3 = new Author("J. D.", "Salinger");

// Create an instance of the class Publisher

        Publisher publisher1 = new Publisher("HarperCollins");
        Publisher publisher2 = new Publisher("Charles Scribner\'s Sons");
        Publisher publisher3 = new Publisher("Little, Brown and Company");

        String[] authors = {author1.toString(), author2.toString(), author3.toString()};
        String[] publishers = {publisher1.toString(), publisher2.toString(), publisher3.toString()};
        String[] books = {book1.toString(), book2.toString(), book3.toString()};

        addBook(authors, publishers, books, "J. K. Rowling", "Bloomsbury", "Harry Potter and the Philosopher\'s Stone", "trash");

        //System.out.println(Arrays.toString(books));
        //displayBooks(books);
       // displayAuthors(authors);
        //displayPublishers(publishers);

    }
    public static void addBook(String[] authors, String[] publishers, String[] books, String author, String publisher, String book, String genre){
        authors = Arrays.copyOf(authors, authors.length + 1);
        authors[authors.length - 1] = author;

        publishers = Arrays.copyOf(publishers, publishers.length + 1);
        publishers[publishers.length - 1] = publisher;

        books = Arrays.copyOf(books, books.length + 1);
        books[books.length - 1] = book;
    }

    public static void displayAuthors(String[] authors){
        for(String author : authors){
            System.out.println(author);
        }

    }
    public static void displayPublishers(String[] publishers){
        for(String publisher : publishers){
            System.out.println(publisher);
        }
    }
    public static void displayBooks(String[] books) {
        for (String book : books) {
            System.out.println(book);

        }
    }
}

Constructor for book:

public class Book {

    String title;
    String author;
    String publisher;
    String genre;

    public Book(String title,String author,String publisher,String genre) {
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.genre = genre;
    }
    public String toString() {
        return this.title + " | " + this.author + " | " + this.publisher + " | " + this.genre;
    }
}

I've tried System.out.println(Arrays.toString(books)); There is no error, so maybe it does happen but when i go to print the array itself it doesnt show up (I'd like all 4 books to show instead of the original 3)

1 Answers1

1

In your addBook method:

authors = Arrays.copyOf(authors, authors.length + 1);
authors[authors.length - 1] = author;

authors is your own local variable. You create a new array here (with copyOf), and assign it to this local copy. You then change something in this array you just made, and then your method ends. With it, your local variable poofs out of existence, and with that, no code has any reference to this copied array. The garbage collector eventually removes it.

The entire method therefore does nothing.

Generally, don't have static anything when you start out (other than your main, which should contain just one line: new MyClass().go(), or possibly new MyClass().go(args) if you care about the args; no further use of static allowed until you're a bit further along in your java career.

If you want things to survive, they have to be fields.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72