Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

help:  toString should _return_ a string, why does it print it?

Thread view: 
javac - 03 May 2004 22:45 GMT
class VectorOfLifeForms toString method:
   public String toString() {
       System.out.println("..toString..");
       for(int i=0; i<MAX_LIFE; i++) {
           lifeForm = (LifeForm)VECTOR_OF_LIFE_FORMS.get(i);
           String lifeFormString = lifeForm.toString();
       }//for
       return "";
   }//toString

class Lifeform toString method:
   public String toString() {
       System.out.println("..LifeForm toString..");
       return "x\t\t" + location.getX() + "\ny\t\t" + location.getY()
+ "\nz\t\t" + location.getZ();
   }//toString

here's my question:  why does the VectorOfLifeForms even print
anything out?

in the main method:
VECTOR_OF_LIFE_FORMS.toString();

thanks,

javac@mail.com
source code:  http://www.geocities.com/cjavacjava/
(now with actual links!)
Woebegone - 04 May 2004 02:54 GMT
> class VectorOfLifeForms toString method:
>     public String toString() {
[quoted text clipped - 24 lines]
> source code:  http://www.geocities.com/cjavacjava/
> (now with actual links!)

What does it print? I'd guess from looking that it prints the line
"..toString.."; this is what you might call a "side effect" of the function
call: it returns the empty String after all that work, but the first
statement in the method is a console print statement. What do you expect?
Stefan Waldmann - 04 May 2004 08:30 GMT
> class VectorOfLifeForms toString method:
>     public String toString() {
[quoted text clipped - 5 lines]
>         return "";
>     }//toString

The toString() method is expected to return a String, so what is it good
for that it only returns an empty String??
( return ""; )

And why are you running through all your LifeForms in your Vector,
create a String from them and then do nothing with it??

Instead you could easily write

public class VectorOfLifeForms {
   public String toString() {
      return VECTOR_OF_LIFE_FORMS.toString();
   }
}

Because the toString() method of Vector (or rather of it's super class
AbstractCollection) does all you want for you - iterates over all it's
elements, calls their toString() method and returns a String composed
the String representations of all your life forms.

(By the way, your Vector is named strangely - only constants which
cannot be changed should be named in uppercase. For a Vector that
doesn't make much sense...)

Now try in your main method

System.out.println(myVectorOfLifeForms.toString());
//                 ^- or however your instance of VECTOR_OF_LIFE_FORMS
//                    is named

Greetings,
Stefan

Signature

Please don't reply to the e-mail address above.

Use instead: stefan DOT waldmann AT web DOT de

javac - 05 May 2004 23:53 GMT
[..]
> public class VectorOfLifeForms {
>     public String toString() {
[quoted text clipped - 6 lines]
> elements, calls their toString() method and returns a String composed
> the String representations of all your life forms.

heh. now i know :)   thanks.

> (By the way, your Vector is named strangely - only constants which
> cannot be changed should be named in uppercase. For a Vector that
> doesn't make much sense...)

my thinking is that LIST_OF_LIFE_FORMS is a singleton, therefore in
all caps. yes/no? the rest i'll have to think about.

[..]

thanks,

javac@mail.com
http://www.geocities.com/cjavacjava
Ryan Stewart - 06 May 2004 00:17 GMT
> [..]
> > public class VectorOfLifeForms {
[quoted text clipped - 16 lines]
> my thinking is that LIST_OF_LIFE_FORMS is a singleton, therefore in
> all caps. yes/no? the rest i'll have to think about.

No. A Vector is not a singleton. A singleton is a class of which there will
be at most one instance. That was a confusing sentence. Turn it around.
There can only ever be one instance of a singleton class. For example:

public class Singleton {
   private static Singleton instance;

   private Singleton() {}

   public Singleton getInstance() {
       if (instance == null) {
           instance = new Singleton();
       }
       return instance;
   }
}

That's a simple, non-thread-safe singleton class and, coincidentally, an
example of a typical getInstance method.
javac - 06 May 2004 23:02 GMT
i did some reading on singleton, think i'm ok with that.

also, i fixed up the toString methods.  i was confused by my own code,
i had System.out in the toString, _and_ was returning a String --  a
mess.  fixed it up, i think :)

thanks all,

javac@mail.com
http://www.geocities.com/cjavacjava/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.