Results 1 to 13 of 13
  1. #1
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Object array toString();

    Hi, there is something missing here, but I am not sure what. I have 3 classes. 1 class has the method
    Java Code:
    public  Name(String x, String y, double z) {
    The 2nd class has the code that accesses the "name" method
    Java Code:
    void addName(String a, String b, doublec)
        {
            data[count] = new nameofClass1(e, f, g);
            ++count;
        }
    And the 3rd class is the where the main method is with a display of this.

    I cannot for the life of me figure out how to to display all the values for data[i].

    I need it to be a toString method. I have tested data[i].x; to make sure that the String passed is located there and it is.

    I am just not able to toString() that whole index of the object array.

    Thanks in advance

  2. #2
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    In summary, why won't data[i].x.toString(); work?

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Object array toString();

    Hi sethe23,

    Can you please explain exactly what you are trying to output from the toString() method. The complete code woud also help to troubleshoot.
    Assuming data[i].x is a String then calling the toString() method explicitly is redundant.

    If you are attempting to display some or all the attributes of the data[i] object you will need to override the toString() method.

    Regards.

  4. #4
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    2 strings and a double. Basically, its a string giving the name, date, and a double value.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Object array toString();

    If its the three parameters you want to print out then you will need to ensure the variables within the object are being updated with the values passed in the Constructor's signature. You will then need to override the toString() method of the Name class.

    From here you will need to call the toString() method from data[i] instead of data[i].x as you have been.

    Regards.

  6. #6
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    So data[i].toString (); I believe I tried and got an error.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Object array toString();

    Post the complete exception, including stack trace, and the relevant area of code it is thrown from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Object array toString();

    Did you override the method?

  9. #9
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    Well its not a runtime error, but it displays name@ and a hex reference

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Object array toString();

    Then you haven't written a toString() method.
    That's not an error, that's the default toString() at work.
    <Classname>@<address>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    Ouch, so what am I missing from this?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Object array toString();

    Apart from a toString() method on whatever class you have in your array?

    Can't tell, as you've not posted much relevant code as far as I can see.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Object array toString();

    Ok, I will post it entirely when I get home.

Similar Threads

  1. Replies: 3
    Last Post: 12-29-2012, 01:21 AM
  2. How to display toString() inside another toString()
    By SVTermiCobra in forum New To Java
    Replies: 1
    Last Post: 08-08-2012, 02:49 AM
  3. Replies: 4
    Last Post: 04-07-2012, 12:55 PM
  4. using the toString method to print an array
    By Wolfie in forum New To Java
    Replies: 1
    Last Post: 03-06-2011, 11:41 PM
  5. Replies: 23
    Last Post: 09-07-2010, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •