Results 1 to 13 of 13
Thread: Object array toString();
- 02-18-2013, 09:29 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Object array toString();
Hi, there is something missing here, but I am not sure what. I have 3 classes. 1 class has the method
The 2nd class has the code that accesses the "name" methodJava Code:public Name(String x, String y, double z) {
And the 3rd class is the where the main method is with a display of this.Java Code:void addName(String a, String b, doublec) { data[count] = new nameofClass1(e, f, g); ++count; }
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
- 02-18-2013, 10:32 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Object array toString();
In summary, why won't data[i].x.toString(); work?
- 02-18-2013, 11:00 AM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-18-2013, 03:46 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Object array toString();
2 strings and a double. Basically, its a string giving the name, date, and a double value.
- 02-18-2013, 04:14 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-18-2013, 04:57 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Object array toString();
So data[i].toString (); I believe I tried and got an error.
- 02-18-2013, 05:07 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 02-18-2013, 05:11 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: Object array toString();
Did you override the method?
- 02-18-2013, 05:52 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Object array toString();
Well its not a runtime error, but it displays name@ and a hex reference
- 02-18-2013, 05:57 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 02-18-2013, 05:59 PM #11
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Object array toString();
Ouch, so what am I missing from this?
- 02-18-2013, 06:09 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 02-18-2013, 07:55 PM #13
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Trying to Remove an Object from an Array List by reading an element of the object
By Alessandro95 in forum New To JavaReplies: 3Last Post: 12-29-2012, 12:21 AM -
How to display toString() inside another toString()
By SVTermiCobra in forum New To JavaReplies: 1Last Post: 08-08-2012, 01:49 AM -
Converting ArrayList<Object> into a String[] of their toString()s
By Psygnosys in forum New To JavaReplies: 4Last Post: 04-07-2012, 11:55 AM -
using the toString method to print an array
By Wolfie in forum New To JavaReplies: 1Last Post: 03-06-2011, 10:41 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks