Hi guys, forgive me if breaking all sorts of laws in Java, I'm only a couple weeks into learning it and I've been struggling the whole way through. Right now what I was trying to do was create an array using the titles of five books and to test to see if my approach was working I tried to output the result. It seems like there's an error in my set/get methods. The code compiles, but my output is null.
So my real question is, can I do what I'm trying to achieve? That is, pull array variables from set/get methods? Is there a better approach or would this work (note: I'm supposed to have get/set methods so those have to stay, I'm struggling with my constructors and arrays at the moment) I'll be working with this some more while I wait for a response so if I figure it out first I'll update with a solved tag and the code I end up using to do so, thanks!
import java.util.Arrays; public class BookStore2 { public static void main(String[] args) { Book Book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f); Book Book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f); Book Book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f); Book Book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f); Book Book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f); String[] titleArray = new String[] {Book1.gettitle, Book2.gettitle, Book3.gettitle, Book4.gettitle, Book5.gettitle}; System.out.println(Arrays.toString(titleArray)); } } class Book{ String ISBN; String title; String name; int year; String publisher; float price; String gettitle; // removed, this must have been part of the problem I was having public Book(String ISBN, String title, String name, int year, String publisher, float price){ this.ISBN = ISBN; this.title = title; this.name = name; this.year = year; this.publisher = publisher; this.price = price; } public void setISBN(String ISBN){ this.ISBN = ISBN; } String getISBN(){ return ISBN; } public void settitle(String title){ this.title = title; } String gettitle(){ return title; } public void setname(String name){ this.name = name; } String getname(){ return name; } public void setyear(int year){ this.year = year; } int getyear(){ return year; } public void setpublisher(String publisher){ this.publisher = publisher; } String getpublisher(){ return publisher; } public void setprice(float price){ this.price = price; } float getprice(){ return price; } // list of getters } // end class book
Updated code:
import java.util.Arrays; public class BookStore2 { public static void main(String[] args) { Book book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f); Book book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f); Book book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f); Book book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f); Book book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f); String[] titleArray = new String[] {book1.gettitle(), book2.gettitle(), book3.gettitle(), book4.gettitle(), book5.gettitle()}; System.out.println(Arrays.toString(titleArray)); System.out.println(book1.gettitle()); } } class Book{ String ISBN; String title; String name; int year; String publisher; float price; public Book(String ISBN, String title, String name, int year, String publisher, float price){ this.ISBN = ISBN; this.title = title; this.name = name; this.year = year; this.publisher = publisher; this.price = price; } public void setISBN(String ISBN){ this.ISBN = ISBN; } String getISBN(){ return ISBN; } String gettitle(){ return title; } public void settitle(String title){ this.title = title; } public void setname(String name){ this.name = name; } String getname(){ return name; } public void setyear(int year){ this.year = year; } int getyear(){ return year; } public void setpublisher(String publisher){ this.publisher = publisher; } String getpublisher(){ return publisher; } public void setprice(float price){ this.price = price; } float getprice(){ return price; } // list of getters } // end class book