Results 1 to 2 of 2
  1. #1
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default How to display toString() inside another toString()

    Ok here is the dilemma (this is the same project as before but different problem)

    I have finished my program but found the output is not what I would like it to be. I have the following lines that display two different toString() methods:

    Java Code:
    System.out.printf("%s", employee1.toString());
    		System.out.printf("%s", benefit1.toString());
    But the thing is I actually need to call the "benefit1" from within the Employee1.toString();

    So how do I do this? My employee's toString method is as follows:

    Java Code:
    public String toString() {
    		NumberFormat nf = NumberFormat.getCurrencyInstance();
    
    		return String.format("\nFirst Name: %s\nLast Name: %s\nGender; %s\nDependents: 
                    %s\nAnnual Salary: %s\nWeekly Pay: %s\nCategory: %s\n", 
                    getFirstName(), getLastName(), getGender(), getDependents(), 
                    nf.format(getAnnualSalary()), nf.format(calculatePay()), category);
    	}
    I need the benefit's toString() output to show up between 'Weekly Pay' and 'Category'
    (Weekly Pay and Category are listed in the codebox above, I need it between those two elements)
    Last edited by SVTermiCobra; 08-08-2012 at 02:39 AM.

  2. #2
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to display toString() inside another toString()

    SOLVED! Don't know why this wasn't working earlier... but now calling it directly works (benefit.toString()), etc...

    Maybe there's something wrong with my Eclipse client

Similar Threads

  1. how to display from toString through subclass
    By SVTermiCobra in forum New To Java
    Replies: 3
    Last Post: 08-07-2012, 11:49 PM
  2. help with toString
    By teardrop3903 in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 03:39 AM
  3. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 03:44 PM
  4. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 09:52 AM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:32 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
  •