• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning user defined objects

 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic