Results 1 to 4 of 4
Thread: Trying to write toString method
- 10-28-2009, 01:50 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Trying to write toString method
Here's my code:
I am getting missing return statement error because it's inside the loop. But i need to transverse the linkedlist to getting string printed.Java Code:public String toString() { Iterator<Term> iter = poly.iterator(); while (iter.hasNext()) { Term term = iter.next(); return term.toString(); } }
-
The compiler is complaining because what happens if polyp has no terms? Then nothing ever gets returned, and that just won't do.
I'm complaining because even if this did work, it would find only ever return the String obtained from the first term. My guess is that you want to combine all the Strings returned, and if so, I'd use a StringBuilder, append the strings to the StringBuilder in the loop, and then after the loop, return the StringBuilder's toString result.
- 10-28-2009, 02:34 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Ya, I did the same thing. You were late by a min.
Thank you very much for help.
-
Doesn't matter if I'm late. What matters is if you've solved your problem. Congrats. If you don't mind, please mark this thread "Solved" before you go.
Similar Threads
-
toString() method
By 01allenh in forum New To JavaReplies: 2Last Post: 03-25-2009, 11:43 PM -
question about writing a toString method
By heather.diggs in forum New To JavaReplies: 2Last Post: 11-12-2008, 11:13 PM -
toString() method of java.security.MessageDigest
By Nicholas Jordan in forum Advanced JavaReplies: 4Last Post: 09-18-2008, 02:32 PM -
Method to write if the string object can be converted to Integer or not.
By Abhishek in forum New To JavaReplies: 4Last Post: 03-25-2008, 12:16 PM -
toString method
By apfroggy0408 in forum New To JavaReplies: 6Last Post: 01-31-2008, 04:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks