9 Replies Latest reply: Sep 18, 2006 12:50 PM by 807598 RSS

    Question about to String method

    807598
      Hi,
      I am trying to figure out how differentiate on which object will use toString() method when I have two different objects returning two different values but pointing to the same class. For example I have two classes from which I am trying to return a String and integer value respectively. In the main method I have
      System.out.print(<object name1>.toString() + "\an"); 
      from which I need to return an integer and I have
      System.out.print(<object name2>.toString() + "\n") 
      from which I need to return a String.
      However my toString() method has
      public  String toString() {
           return      "\nThe answer is : " + <value1>;
      This is what I have commented out as I do not know how to point two different objects toString () method or should I declare another toString() method?
      \\ "\nThe answer is : " + <value2>;
      (this is to return a string but how can I make toString() to read from both methods? I have
      public static int <name>( String <name>, int <name>) { }
      that returns String and
      public static String name ( int <name> , int <name>) {} 
      that returns int. In the main method I have declaration of
      <class name> <object name 1>, <object name 2>; 
      pointing to the class I am trying to invoke toString() method on.
      Thanks
      Bart
        • 1. Re: Question about to String method
          3004
          I don't really understand your question.

          toString always returns a String. It's always called on the object on which you call it.
          • 2. Re: Question about to String method
            807598
            I too, am really confused with your question. Are trying to overload a method so that it could return either a String or an int depending on the context?
            the toString method always returns a String, even if you do not override it.
            Your code is hard to follow because of your top secerete object, method, and variable names.

            JJ
            • 3. Re: Question about to String method
              807598
              Hi,
              Yes it does return the String. But if you have two objects to point to one toString() method how do you do that? I mean if the main method let's say have:
              1. Please enter the value to change your balance:
              2. Please enter the value to rename your account:
              Those two questions point to the same class let's say Bank but each of them returns different values. However would you use toString() method to be able to return the value for either one of those methods in the Bank class?
              I hope this clarifies some.
              Thanks
              Bart
              • 4. Re: Question about to String method
                796447
                must refrain from saying something rude here

                But can I just say instead that I knew someone who wanted to be an engineer, but it turned out that he naturally fit into a different career path instead. He would have been miserable as an engineer.
                • 5. Re: Question about to String method
                  3004
                  Hi,
                  Yes it does return the String. But if you have two
                  objects to point to one toString() method how do you
                  do that?
                  "Two obejcts to point to one toString method"? Objects don't point to methods.


                  I mean if the main method let's say have:
                  1. Please enter the value to change your balance:
                  2. Please enter the value to rename your account:
                  Those two questions point to the same class let's say
                  Bank but each of them returns different values.
                  Again "pointing to the class"?

                  And those aren't methods, they're just questions. How about posting some actual code?


                  However would you use toString() method to be able to
                  return the value for either one of those methods in
                  the Bank class?
                  You wouldn't. If you want the class to expose its internal state, you'd use getter methods. But you probably wouldn't store a change to an account in a member variable anyway.
                  • 6. Re: Question about to String method
                    807598
                    Hi,
                    Yes it does return the String. But if you have two
                    objects to point to one toString() method how do you
                    do that? I mean if the main method let's say have:
                    1. Please enter the value to change your balance:
                    2. Please enter the value to rename your account:
                    Those two questions point to the same class let's say
                    Bank but each of them returns different values.
                    However would you use toString() method to be able to
                    return the value for either one of those methods in
                    the Bank class?
                    I hope this clarifies some.
                    Thanks
                    Bart
                    You are confusing classes, objects, reference variables and methods.
                    The toString method will always return a String representation of your object. You must override this method if you want anything different from it's super class'es toString definition (Object class by default).
                    If you want your Bank class to return the account balance, then you should write a method that returns the account balance like, say, getBalance() for instance.

                    JJ
                    • 7. Re: Question about to String method
                      807598
                      To one of the comments above ?????
                      ***** One thing I need to say here. Even engineer can be worth nothing if all he has is book knowledge by calling himself smart and no common sense and open mind. ***** Eager to learn is more than being surrounded by those who think they know more than they should have ******
                      Cheers to all of those who have their minds open and keep progressing on contrary to those who think they know everything because they do not!!
                      Thanks
                      Bart
                      • 8. Re: Question about to String method
                        807598
                        To one of the comments above ?????
                        ***** One thing I need to say here. Even engineer can
                        be worth nothing if all he has is book knowledge by
                        calling himself smart and no common sense and open
                        mind. ***** Eager to learn is more than being
                        surrounded by those who think they know more than
                        they should have ******
                        I thought there were real swear words. Only asterisks. sigh
                        • 9. Re: Question about to String method
                          807598
                          He (warnerja) is just frustrated because you're not making any sense, and you seem convinced that you are making sense, and you seem unwilling to acknowledge even the possibility that you're not making any sense. He's not saying that he thinks he knows everything.

                          The toString method returns a string representation of the object. That's all. It doesn't make any difference what's calling it.

                          If you want toString to return a value other than a String, or if you want it to return different strings based on what's calling it or based on anything other than the current state of the object...then you really don't want toString.

                          What are you trying to accomplish? Don't try to explain it in terms of some imagined implementation -- just tell us what you're trying to do.