Results 1 to 4 of 4
  1. #1
    curious is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Trying to write toString method

    Here's my code:

    Java Code:
    public String toString()
        {
            Iterator<Term> iter = poly.iterator();
            while (iter.hasNext())
            {
                Term term = iter.next();
                return term.toString();
            }  
        }
    I am getting missing return statement error because it's inside the loop. But i need to transverse the linkedlist to getting string printed.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    curious is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Ya, I did the same thing. You were late by a min.

    Thank you very much for help.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  2. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-12-2008, 11:13 PM
  3. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 02:32 PM
  4. Replies: 4
    Last Post: 03-25-2008, 12:16 PM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 AM

Posting Permissions

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