Link to home
Start Free TrialLog in
Avatar of AdamJSawyer
AdamJSawyer

asked on

Help with constructor problem

I have the following code in Catalogue.java:

   public void addBook(String addTitle, String addAuthor, String addYear)
   {      
      array = books.length;
     
      books[array] = new Book(addTitle, addAuthor, addYear);
   }

It complains on the last line when trying to add a new book to the array.

The constructors in Book.java are:

   public void Book()
   {
     
      title = "";
      author = "";
      year = "";
     
   }
   
   
   public void Book(String aTitle, String aAuthor, String aYear)
   {
     
      title = aTitle;
      author = aAuthor;
      year = aYear;
                 
   }

Can someone tell me why this doesn't work?

thx
adam
Avatar of sciuriware
sciuriware

Delete the word void in the constructor.

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AdamJSawyer

ASKER

The error returned by JCreator is:

--------------------Configuration: DaRock - j2sdk1.4.1 <Default> - <Default>--------------------
C:\Documents and Settings\Adam\My Documents\!DaRock\Catalogue.java:34: cannot resolve symbol
symbol  : constructor Book (java.lang.String,java.lang.String,java.lang.String)
location: class Book
      books[array] = new Book(addTitle, addAuthor, addYear);
                            ^
1 error

Process completed.
Did you delete the word 'void' in 2 constructors?

Then:

      array = books.length;
     
      books[array] = new Book(addTitle, addAuthor, addYear);

Will fail: obviously 'books' has a length of 'array', so the last index is array-1.

;JOOP!
................ and heed my last remark.

;JOOP!
sorry for lack of feedback. Yeah I fixed those constructors and then fiddled the code to work. the guy I was helping was new to programming and wasn't sure about arrays. I set the array to a predetermined size with a constant, and used an index to point to the place the next book was to be created. Thanks for you help

Cheers
Adam