toString format help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 37
Reputation: bondo is an unknown quantity at this point 
Solved Threads: 0
bondo's Avatar
bondo bondo is offline Offline
Light Poster

toString format help

 
0
  #1
May 1st, 2007
Dear god. this is driving me crazy. I'm working on an assignment involving inheritence, multiple classes, etc. I'm stuck at one stupid part. I have to write a toString method that formats the text. Is there a way to return a string with formatted text? I know about the printf method, and I can get that to work fine, but just with System.out.printf... I don't know how to return a formatted string.. Is it even possible?

What i need is I have someones name, first and last, then their age, but the ages of the people have to line up in a column. Make sense? The names & ages are one object, & the multiple people are in array of people objects... so i have a for loop that will go through and print each person one at a time. So how do i pad the age correctly & return it to the calling method?

I would just say forget returning the string and just simply print it out, but I need the same method elsewhere to write to a file also.

so, what i need to end up with is something that looks like this

[html]
Name Age other stuff more stuff
-------------------------------------------------------------------------------
Bob Someguy 42 blabado 3.14
Guy Two 38 dododod 1234
[/html]
nice neat columns n' stuff...
I appreciate any thoughts...
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 37
Reputation: bondo is an unknown quantity at this point 
Solved Threads: 0
bondo's Avatar
bondo bondo is offline Offline
Light Poster

Re: toString format help

 
0
  #2
May 1st, 2007
BLAH! Ok, i should've done a search first. I found my answer.

For anyone else who need's help on this same thing, here's the thread I found:

http://www.daniweb.com/techtalkforum...ad.php?t=56466
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC