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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The error returned by JCreator is:
--------------------Config uration: DaRock - j2sdk1.4.1 <Default> - <Default>----------------- ---
C:\Documents and Settings\Adam\My Documents\!DaRock\Catalogu e.java:34: cannot resolve symbol
symbol : constructor Book (java.lang.String,java.lan g.String,j ava.lang.S tring)
location: class Book
books[array] = new Book(addTitle, addAuthor, addYear);
^
1 error
Process completed.
--------------------Config
C:\Documents and Settings\Adam\My Documents\!DaRock\Catalogu
symbol : constructor Book (java.lang.String,java.lan
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!
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!
;JOOP!
ASKER
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
Cheers
Adam
;JOOP!