Perry Smith

Java Questions & Answers

Home / Java Q & A Archive Page 3 / Java Q & A Archive

Help with "toString" method
 Thursday, 21-Jan-99 17:12:04

      209.162.144.218 writes:

      Okay, I need to provide a "toString" method for
      a class so that I can see the value of an integer printed out. The catch is that I'm
      not allowed to use any of the Java provided methods.

      Help, please?



      Mephisto


Responses
Re: Help with "toString" method
 Thursday, 21-Jan-99 17:35:47

      24.1.16.214 writes:

      huh? I don't understand what you mean by not
      being 'allowed to use any of the Java provided
      methods'...

      class Foo
      {
              private int someInt;
              Foo (int newValue)
              {
                      someInt = newValue;
              } // constructor Foo
              public String toString()
              {
                      return (String)someInt;
              }
      }
      a class, Foo, has an instance variable of type
      int, someInt.  a method of Foo, toString, returns
      someInt cast as a String.
      -calyxa


      calyxa



Re: Re: Help with "toString" method
 Thursday, 21-Jan-99 18:22:34

      209.162.144.112 writes:

      I second your 'huh'?

      The way I understand it, I am not allowed to
      do:

      String s = String.valueof(integer);

      However, I am not quite sure what is
      considered "Java provided methods". This is
      something I'll have to ask the prof.

      For now I am doing a slight variation of what you
      suggested. Mainly:

      public String toString() {
      return "" + intValue;
      }

      Seems crude, but it works. Now I have to figure
      out how to do the method parseInt. Thanks for
      your time.

      Mephisto



Home / Java Q & A Archive Page 3 / Java Q & A Archive