• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return a string as a class object

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm creating a series of classes/methods with a given Main, which can't be ammended. In Main there's the below line:

MeetingGroups us =mm.listAll(st[1]);
(...)
this.getOut().println(us);

MeetingGroups is the class, and the listAll method must return a MeetingGroup object. However, it must print a string with info from attributes from the class. Anyone knows how can I return a String as a class object?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:and the listAll method must return a MeetingGroup object. However, it must print a string with info from attributes from the class. Anyone knows how can I return a String as a class object?


Your last question is NOT what you've been asked to do. In fact, it's the exact opposite: You've been asked to return a "class" object as a String.

Now without giving too many hints, what method, common to ALL Java objects, do you think might do that?

And if you still don't understand, come back.

Winston
 
Ciro Vilchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The listAll method must return a MeetingGroup object. I can't use the toString() method because it complains I'm returning a String. I'm missing something but still can't figure out what it is.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:MeetingGroups is the class, and the listAll method must return a MeetingGroup object. However, it must print a string with info from attributes from the class. Anyone knows how can I return a String as a class object?



So it has to return a MeetingGroup object, and it has to print a string. I don't see any conflict between those two separate requirements. Don't try to combine them into a single requirement -- in other words, there's no requirement to return a String object.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:
I can't use the toString() method because it complains I'm returning a String.



Would you care to elaborate? What exactly is this complaining?
 
Ciro Vilchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for the replies. I probably haven't explained very well. Let's say I have the below line in the Main class:



Now, in the MeetingGroup class:




With the println() method, it will call by default the toString() method in MeetingGroup, so if I don't override it it will just print the name of the class MeetingGroup, but I want it to print just the attribute "someOutput". Can someone help on this?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:Hi, thanks for the replies. I probably haven't explained very well. Let's say I have the below line in the Main class:
...
With the println() method, it will call by default the toString() method in MeetingGroup, so if I don't override it it will just print the name of the class MeetingGroup, but I want it to print just the attribute "someOutput". Can someone help on this?


This is kind of what I suspected.

println() will ALWAYS call your object's toString() method if the supplied object isn't a String. So, you have three choices:
1. Override toString() to do what you want.
2. Create another method (getSomeOutput()?) to return the value, and call that in your print statement.

Winston
 
Ciro Vilchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes I know I need to override the toString() method in the MeetingGroup class. The problem is, I don't know how to do it so it returns the desired output, I have nothing in my notes to do that.

Modifying the Main class is out of the question, this is an assignment and I must leav ethe Main class untouched.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:Yes I know I need to override the toString() method in the MeetingGroup class. The problem is, I don't know how to do it so it returns the desired output, I have nothing in my notes to do that.


So you're on your own.

Questions:
1. What is the type of 'someOutput'?
2. What type is returned by toString()?
3. (maybe most important) What have you tried so far?

Winston
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciro Vilchez wrote:
Yes I know I need to override the toString() method in the MeetingGroup class. The problem is, I don't know how to do it so it returns the desired output, I have nothing in my notes to do that.


Nobody is going to just give you the answer here. That's not how this site works. We give you tips but you have to think and do the work. If you have nothing in your notes about it, did you try looking elsewhere for information?
 
Ciro Vilchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want anyone doing the work for me, what I need is to know how it's done, I've been looking for guidelines but I just can't fine any info on this subject. If someone could point me to some site I'd be very grateful.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Searching for overriding toString in Java turns up numerous examples. Did none of these help you?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or maybe Java toString tutorial? Also, Winston asked you to share what you have tried. We can give you more suggestions if you let us see what you've tried so far.
 
Ciro Vilchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank guys, I've solved by overriding the toString method. The problem was that I had a third class involved, so it took a bit of an extra work.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic