• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

returning user defined objects

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic