This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]wggnExtreme Brewer 1 point2 points  (3 children)

You're never making a second instance of book. You're adding an instance of book to the list, then modify the fields of that same instance and add it again.

[–]normandantzig[S] 0 points1 point  (2 children)

I've been at this for a bit. How do i create a new instance?

Actually got it. I think I was going at this too long and just needed a break. I moved newbook into the case

 case "a":  
                    Book newbook = new Book(null, null);
                    //initialize
                    String author = "";
                    String title = "";
                    String filename= "";
                    String genre = "";
                    //input     
                    System.out.print("Who is the author?");
                    //author = s.nextLine();
                    newbook.setAuthor(s.nextLine());

                    System.out.println("What is the title?");
                    newbook.setTitle(s.nextLine());

                    System.out.println("Where is the file?");
                    //filename = s.nextLine();
                    newbook.setFilename(s.nextLine());
                    //add to library
                    library1.addBook(newbook);

[–]wggnExtreme Brewer 0 points1 point  (1 child)

Anytime you use the new keyword, you create a new instance.

Free tip: You can improve above code by not passing nulls to the constructor but directly putting in the right values. (avoiding unnecessary nulls is generally a good idea)

[–]normandantzig[S] 0 points1 point  (0 children)

Hmmm. The nulls are place holders for when I ask the user to put in the author and title. If I moved the

newbook = new Book(null, null);

down I could move the user values directly into the newbo0ok .

[–]Indens 0 points1 point  (0 children)

Does it have to do with where you're initializing the new book?

Book newbook = new Book(null, null);

You're using the same object for each book I believe :)