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
Lord Zoltar - 29 Apr 2008 19:30 GMT
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 10 lines]
>
> Cat
Write a string padding function. Java doesn't have one, so you'd have
to write your own. Search on this group for "string padding"
I found this:
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/66820
0f921a2c882/2d61ff32f3278915?lnk=gst&q=string+padding#2d61ff32f3278915
Mark Space - 29 Apr 2008 22:03 GMT
>> How can I implement this right-justified, blank padded, fixed-width filed output in Java?
>>
>> Cat
>
> Write a string padding function. Java doesn't have one, so you'd have
*coughFormattercough*
<http://java.sun.com/javase/6/docs/api/java/util/Formatter.html>
Knute Johnson - 29 Apr 2008 22:42 GMT
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 10 lines]
>
> Cat
System.out.printf("%12s","some string");

Signature
Knute Johnson
email s/nospam/linux/
Arne Vajhøj - 30 Apr 2008 00:12 GMT
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 8 lines]
>
> How can I implement this right-justified, blank padded, fixed-width filed output in Java?
System.out.printf("%12s", s);
Arne
Christian - 30 Apr 2008 00:42 GMT
Catherina Svensson schrieb:
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 10 lines]
>
> Cat
have you tried:
String.format("%12s","aaa");
Christian
Chase Preuninger - 30 Apr 2008 02:25 GMT
The idea of Console output is to be simple. To format text you need
something much more than just UTF bytes for example HTML. Simply you
can't and nor does it make sense to do so. It is up to what is
displaying the characters to interpret what to display.
petersprc - 30 Apr 2008 03:13 GMT
Hi,
System.out.format("%12s\n", "aaa");
Regards,
John Peters
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 10 lines]
>
> Cat
Andrea Francia - 30 Apr 2008 20:11 GMT
> Hi,
>
> System.out.format("%12s\n", "aaa");
Instead of using '\n' is better use the '%n' that is expanded to correct
line feed for the current platform.

Signature
Andrea Francia
alias rm='trash' #use trash command instead of removing
rm -Rfv file #put the file in the KDE trashcan
http://www.andreafrancia.it/trash
alexandre_paterson@yahoo.fr - 30 Apr 2008 04:21 GMT
> Lets say I have a String var which can contain values of different lengths e.g.
> "aaa" or "bb bb bb".
[quoted text clipped - 8 lines]
>
> How can I implement this right-justified, blank padded, fixed-width filed output in Java?
What if the field is bigger than 12 chars?
Anyway Jakarta commons contains a lot of very useful stuff, including
a lot of StringUtils
very convenient methods, including leftpad, etc.
If you want don't want to use Jakarta commons for this, you may write
a helper method
somewhere in your project.
The following may help (not tested):
private static final String PAD = " ";
final String s ="aa aaa aa";
System.out.println( PAD.substring( Math.min(PAD.length(),
s.length())) + s);
Roedy Green - 30 Apr 2008 06:04 GMT
>Lets say I have a String var which can contain values of different lengths e.g.
>"aaa" or "bb bb bb".
[quoted text clipped - 10 lines]
>
>Cat
see http://mindprod.com/jgloss/printf.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Donkey Hot - 30 Apr 2008 17:48 GMT
> Lets say I have a String var which can contain values of different
> lengths e.g. "aaa" or "bb bb bb".
[quoted text clipped - 11 lines]
>
> 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);
}
}
thufir - 30 Apr 2008 23:08 GMT
> package confusing;
>
[quoted text clipped - 8 lines]
> }
> }
I don't think that this will meet the requirements of the assignment, not
all of which are (probably) stated.
I bet that there needs to be a loop of some sort, probably nested, and
some arrays. Perhaps an array of Strings? As the array is iterated,
depending on the index, the printing is different.
-Nick