How to right-justify a single column of my console output in Java -


i've been asked create program takes items text file , displays them in list formatted "lastname, firstname zipcode" zip codes align on right , last , first names separated comma , space.

i'm having no problem getting list, problem i'm running aligning zip codes.

currently have written output format:

system.out.printf("%s, %s %s\n", filelist.get(lastname), filelist.get(i), filelist.get(zipcode)); 

and output (as expected):

baron, steven 84521 chill, robert 63258 blanthony, stacy 84815 wick, john 78412 yule, katherine 42136 

(etc)

here's how want look:

baron, steven    84521 chill, robert    63258 blanthony, stacy 84815 wick, john       78412 yule, katherine  42136 

i thinking maybe count total number of characters lastname way zip code , have subtracted large number of spaces between lastname , zipcode, wasn't sure how since bouncing between strings , ints. also, imagine there simpler solution i'm not aware of.

any appreciated!

thank you,

ccoolant

got word on 1 , got answer may others in future! depends on setting arrays used in different way, of nonetheless. concatenating first , last name so:

string lastnameandfirstname = (lastname + ", " + firstname); 

you can control total width of both of these variables making them single column of width determined you. instead of managing 3 columns can use 2 , like:

system.out.printf(%-20s %s, lastnameandfirstname, zipcode); 

this should give example list presented above:

baron, steven    84521 chill, robert    63258 blanthony, stacy 84815 wick, john       78412 yule, katherine  42136 

this way names on left left-justified (which looks nice) , numbers on right right-justified.

hopefully else finds of use! :)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -