Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes returning user defined objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning user defined objects" Watch "returning user defined objects" New topic
Author

returning user defined objects

James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
I am trying to save book1 objects into an array of book1 instances within the book class and would like to save the titles of the books in the array. I wonder if someone could explain to me how you associate what is inherently a string to a book1 object


thankyou for your input in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

The following code...



basically sets every member of the books array to null. You are indeed creating book1 objects, but you are not storing them. Instead, you use it to get the title object stored internally in the book1 object. Since this object is not set, you get null. And effectively set the members of the array to null.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
thanks for your reply henry, I do realise that they are set to null at the moment (sorry I should have said that earlier) .What is confusing me is how do you set what is an book1 object to save a title ;
ie if the method in book1 was declared as public String getTitle()and title was declared as String and you tryed to save it into a book1 object you get a found string expected book1,how do I get around this problem . I want the title of the book to be saved in the the array even though they are going to be all the same at the moment just getting them in the array would be a start . thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by James .D.Johnstone:
What is confusing me is how do you set what is an book1 object to save a title ;
ie if the method in book1 was declared as public String getTitle()and title was declared as String and you tryed to save it into a book1 object you get a found string expected book1,how do I get around this problem . I want the title of the book to be saved in the the array even though they are going to be all the same at the moment just getting them in the array would be a start . thanks


Sorry, you lost me... You are trying save a title in a book object? But you can't use strings? ... That is as far as I got, and even then, I am not sure what you are trying to do.

Please elaborate... thanks.
Henry
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ah, I think just change your array declaration to String[].

You might change the variable name from books to "titles" to help the next reader know what's in there.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
The array that i am creating must be of book1 type.I want to create the objects in the book class.each object must have a title assigned to it ,then it has to be printed to screen.I am unsure how to get the book1 objects in the array to hold the title of the books that I will eventually pass to them.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Originally posted by James .D.Johnstone:
The array that i am creating must be of book1 type.I want to create the objects in the book class.each object must have a title assigned to it ,then it has to be printed to screen.I am unsure how to get the book1 objects in the array to hold the title of the books that I will eventually pass to them.


It took a while, but it finally clicked...

Anyway, change the title variable in your book class to String, as you are storing a string. When you populate the array, do *not* call the getTitle() method. Simply store the object that you create into the array.

Later, when you need to print to the screen, call the getTitle() method to get the title then.

Is that what you are looking for?

Henry
 
wood burning stoves
 
subject: returning user defined objects