Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: How can I w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 13 Topic 16039 of 16269
Post > Topic >>

Re: How can I write right-justified into a 12 chars width field?

by Donkey Hot <spam@[EMAIL PROTECTED] > Apr 30, 2008 at 04:48 PM

gremlin@[EMAIL PROTECTED]
 (Catherina Svensson) wrote in
news:4817678f$0$7540$9b4e6d93@[EMAIL PROTECTED]
 

> Lets say I have a String var which can contain values of different
> lengths e.g. "aaa"  or "bb bb bb".
> 
> I want to print them out with System.out.println right-justified with
> a field width of 12 chars. If necessary this field should be left
> padded with blanks. For the samples above the output should be e.g.:
> 
> "         aaa"
> and
> "    bb bb bb"
> 
> How can I implement this right-justified, blank padded, fixed-width
> filed output in Java? 
> 
> Cat
> 
> 

package confusing;

public class Main
{
    public static void main(String[] args)
    {
        String blank = "            ";
        String sample = "bb bb bb";

        System.out.println(blank.substring(0,
    	    	blank.length() - sample.length()) + sample);
    }
}
 




 13 Posts in Topic:
How can I write right-justified into a 12 chars width field?
gremlin@[EMAIL PROTECTED]  2008-04-29 18:23:11 
Re: How can I write right-justified into a 12 chars width field?
Lord Zoltar <lord.zolt  2008-04-29 11:30:08 
Re: How can I write right-justified into a 12 chars width field?
Mark Space <markspace@  2008-04-29 14:03:30 
Re: How can I write right-justified into a 12 chars width field?
Knute Johnson <nospam@  2008-04-29 14:42:26 
Re: How can I write right-justified into a 12 chars width field?
=?ISO-8859-1?Q?Arne_Vajh=  2008-04-29 19:12:28 
Re: How can I write right-justified into a 12 chars width field?
Christian <fakemail@[E  2008-04-30 01:42:37 
Re: How can I write right-justified into a 12 chars width field?
Chase Preuninger <chas  2008-04-29 18:25:40 
Re: How can I write right-justified into a 12 chars width field?
petersprc <petersprc@[  2008-04-29 19:13:04 
Re: How can I write right-justified into a 12 chars width field?
Andrea Francia <andrea  2008-04-30 21:11:43 
Re: How can I write right-justified into a 12 chars width field?
alexandre_paterson@[EMAIL  2008-04-29 20:21:46 
Re: How can I write right-justified into a 12 chars width field?
Roedy Green <see_websi  2008-04-30 05:04:11 
Re: How can I write right-justified into a 12 chars width field?
Donkey Hot <spam@[EMAI  2008-04-30 16:48:13 
Re: How can I write right-justified into a 12 chars width field?
thufir <hawat.thufir@[  2008-04-30 22:08:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 5:01:54 CDT 2008.