0

OK, so we had to create like a bookshelf and book objects with publisher, author, copyright date, etc. on a seperate file (basically creating an object for a driver class). I've redone this twice now this week and still get the same error. Perhaps someone could point me in the right direction? Maybe I'm missing something, I'm not sure. EDIT: the problem is: sorry I'm getting ready for work and going ahead of myself I have three: first the output is giving me: Book@15db9742 Book@6d06d69c then the book class for the year is red saying to convert from a string to an int so I do then it wants to go back the other way

    public class Book {
    private String title;
    private String author;
    private String publisher;
    private int copyDate;

    public Book(String bookTitle, String authorName, String publisherName, int bookYear){
        title = bookTitle;
        author = authorName;
        publisher = publisherName;
        copyDate = bookYear;

    }

    public Book(){
        title = "book titler";
        author = "author name";
        publisher = "book publisher";
        copyDate = "2014";


    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String bookTitle) {
        title=bookTitle;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String authorName) {
        author = authorName;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisherName) {
        publisher = publisherName;
    }

    public int getCopyDate() {
        return copyDate;
    }

    public void setCopyDate(int copyDate) {
        this.copyDate = copyDate;
    }


}

Here is the main class:

public class BookShelf {

    public static void main(String[] args) {

        int bookYear = 2014;
        Book name1 = new Book("book\n", "something\n", "something else\n", bookYear);
        Book name2 = new Book("anotherBook\n", "anotherSomething\n", "somethingElse^2\n", bookYear);

        System.out.println(name1);
        System.out.println(name2);
    }

}

I entered arbitrary information so I can make sure it works before I go searching for a good book to enter the information for lol.

Thank you!

wwe9112
  • 43
  • 8
  • 1
    Please include the "error" you're getting. – rgettman Oct 28 '14 at 19:46
  • sorry I'm getting ready for work and going ahead of myself I have three: first the output is giving me: Book@15db9742 Book@6d06d69c then the book class for the year is red saying to convert from a string to an int so I do then it wants to go back the other way. – wwe9112 Oct 28 '14 at 19:47
  • this is the default return values of `toString` of the class `Object`, you need to override it in your class and give it a meaningful implementation. – A4L Oct 28 '14 at 19:48
  • instance fields in book's constructor don't have the keyword 'this' – Christian Cabrejo J. Oct 28 '14 at 19:50
  • *the book class for the year is red*: you need to learn how to use your IDE. Every IDE has a view listing the compilation errors and warnings, as text. You MUST read these error messages, and post them if you don't understand them. Just knowing that a class "is red" is not sufficient. The error message tells you what and where your problem is. – JB Nizet Oct 28 '14 at 19:50

2 Answers2

6

I think you need to override the toString() method so System.out.println() prints the information you want about a book object.

By default Object's toString() doesn't print any fields but instead:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

How to use the toString method in Java?

This method would go in the class (each class has it's own `toString() implementation)

class Book{
   ...
   @Override
   public String toString(){
       //feel free to change this sting to whatever you want
       return "Book " + title;
   }

}

Also as Andreas Jabusch in the other answer, your constructor sets copyDate incorrectly. It should be an int

copyDate = 2014

Get rid of the quotes which makes it a String.

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • Would that go in the class or in the main? I'm sorry, I feel like an idiot, I dont even see this in the book up to the point of where we are -_-. Thank you for helping me. – wwe9112 Oct 28 '14 at 19:54
  • Book [title=book , author=something , publisher=something else , copyDate=2014] Book [title=anotherBook , author=anotherSomething , publisher=somethingElse^2 , copyDate=2014] is that what it would output? It looks like a debugging type thing; so I am assuming if this is what it would output then that would make sense considering the next section is debugging. But again I am not sure; this is my first programming class, nor do I have any experience. – wwe9112 Oct 28 '14 at 20:08
  • toString is often used during debugging. You can put as much or as little as you want. if you want to print out info about the book in a program you should NOT use toString() but instead rely on the book's getter methods to get the values you want and make a new string with those. – dkatzel Oct 28 '14 at 20:39
0

Your

copyDate = "2014"

has to be

copyDate = 2014

it's an int, not a String.

Andreas Jabusch
  • 109
  • 2
  • 9
  • Ah thank you lol. I cant believe I did that lol. Sadly, I did it multiple times and I should have known not to. Thank you! – wwe9112 Oct 28 '14 at 20:08