• Post Reply Bookmark Topic Watch Topic
  • New Topic

align output with StringBuffer  RSS feed

 
Ellen fish
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I couldn't align my output with /t.
For example,
StringBuffer str = new StringBuffer();

str.append("out.txts\t\t"+"a\t"+"b\t"+"c\t"+"\n");
.
.
.
str.append("out.txt\t\t"+"a\t"+"b\t"+"c\t"+"\n");

output:
out.txts a b c
out.txt a b c

Is there a way for me to align my out put so it become


I have been looking into java formater but couldn't find anything related to string buffer.

Any help or suggestion will be appreciated.

Thank you so much

[ December 05, 2008: Message edited by: Ellen fish ]
[ December 05, 2008: Message edited by: Ellen fish ]
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output:
out.txts a b c
out.txt a b c

Is there a way for me to align my out put so it become
out.txts a b c
out.txt a b c


Whats the difference?
 
Ellen fish
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.txts a b c
out.txt a b c

and

so the a b c etc. allgin and spaced by tab
[ December 05, 2008: Message edited by: Ellen fish ]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, you either have to use something that can format your output. Or you have to "format" it yourself, by painstakingly keeping track of your output lengths (and where the tabs markers are) and decide whether you need one or two tabs, etc. (StringBuffer does not have such a facility built in)

Henry
[ December 05, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringBuffer; use StringBuilder instead, unless you need the synchronization.

What about the static format method of String?
 
Ellen fish
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any idea how I can use the format?
I'm researching on it but I need it quite urgent.

Thank you
[ December 05, 2008: Message edited by: Ellen fish ]
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked out how to use the % tags for ordinary output yet? Have you been through the little bit in the Java Tutorials, or the details in the Formatter class?

System.out.printf("%c%20s is %-20s%c", '*', "Campbell", "brilliant", (char)0x2a);

And don't say "urgent."
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any luck yet?
 
Ellen fish
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, the formater seem quite hard to apply in my program because I need to print strings in order. So I mit be using StringBuilder and keep track of the string length. Couldn't find a better way to do it.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think String.format might serve you well.
 
Ellen fish
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very very much.
String format = "|%1$-30s|%2$-10s|%3$-20s|\n";
String s = String.format
(format, "a", "b", "c");

String.format works exactly as I wanted.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I write a program using String.format this is what the output I'm getting.

class StringFormatting
{
public static void main(String[] args)
{
String format = "|%1$-30s|%2$-10s|%3$-20s|\n";
String s = String.format
(format, "a", "b", "c");
System.out.println("Formatted String is "+s);
}
}




C:\sai>javac StringFormatting.java

C:\sai>java StringFormatting
Formatted String is |a |b |c
|

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use printf that is also working fine.
But I don't know what is printf(I remember we use it in C)


class StringFormatting
{
public static void main(String[] args)
{
String ramya="ramya";
String brilliant="brilliant";
String format = "|%1$-30s|%2$-10s|%3$-20s|\n";
//String s = String.format(format, "a", "b", "c");
System.out.printf("%c%20s is %-20s%c", '*', "Campbell",

"brilliant", (char)0x2a);
System.out.println();

//System.out.printf("%c%10s is %-10s%c", '*', "Ramya",

//brilliant", (char)0x2a);

System.out.printf("%c%20s is %-20s%c", '*', ramya, brilliant, (char)0x2a);
//System.out.println("Formatted String is "+s);

}
}


output:




But how to resonate this feature this requirement



Regards.

[edit]Alter quote tags to code tags to preserve spaces. CR[/edit]
[ December 08, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print the out.txt and outfile.txt so that a b c following are aligned, all you have to do is give a width for that field wider than characters in "outfile.txt" (11 characters).
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 4 methods I know of which can do that sort of formatting: Formatter#format PrintStream#format PrintStream#printf and (static) String#format; they all have similar overloading. I believe they all use the Formatter method. There are more details in links I provided previously.

And please use code tags to preserve spacing, not quote tags. I have edited your post and the formatting with the correct alignment in columns has reappeared.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I don't know what we're doing in the following code, I'm now able to understand when you said
all you have to do is give a width for that field wider than characters in "outfile.txt" (11 characters).

How to do it?


I understand % is used to specify the position
what $,s and -10,-20,30 specify?

When I modify my code this way,

I get the following output:

It's not looking good.
I'm frustrated on using this formatter.
Lord campbell help me!
Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Also explain what does this mean?

2)How we can use printf statements which is proprietary to C.
Is this a native code?
Regards.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printf function is not being used. I am not convinced that there is such a thing as "proprietary to C;" is C a "proprietary" language in the first place?
There is a printf method which was introduced in Java5, and I have already given links to where you can read about it, and the format methods. It tells you what -20 means, and why you ought to write %n rather than \n.
I don't think it is a native method, but you can find out by finding the src.zip file, which resides somewhere in your Java installation folders, unzipping it to some other convenient location, then looking in the java folder, then the util folder, to find the code of the Formatter class. You can tell by reading that.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ellen fish:
Thank you very very much.
String format = "|%1$-30s|%2$-10s|%3$-20s|\n";
String s = String.format
(format, "a", "b", "c");

String.format works exactly as I wanted.


You're welcome. Sorry I didn't notice that post earlier. The 1$ 2$ and 3$ bits are unnecessary; you only need them when you aren't printing the arguments in order. And it is better to use %n than \n; it gets the correct platform-specific line end combination.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is simple:

The output is :


I want the ouput to be in this format:


Al is 45 years old
Al23455 is 45 years old
Al23455 is 459999 years old

How to do that?
If I could not get this output in this format, then it is futile to try this String.format.
Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I want the output to be in this format:

Al is 45 years old
Al23455 is 45 years old
Al23455 is 459999 years old

I tried using 10s,11s but it was not yielding the desired result.


The output is:

But this is no way near the desired result.
Regards.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't hi-jack Ellen Fish's thread by posting new questions on it; your question ought to be in a new thread.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear campbell,
I've not hijacked fish's thread.
Whatever question I've asked is pertinent to his question , on which I've worked on & if at all I need any clarifications I post.
I hope you will understand this.
Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any replies?
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have asked a new question, so please start a new thread.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I've finally found it out how to get the desired output.
I thank all the people.
I've used printf method of printstream in achieving this.


output:


Try it out.
%20s--> specifies that in that position which 20 spaces from the beginning a string value(specifically last letter of the first string) is going to come so that it can facilitate the alignment of subsequent strings .
Consider these are the strings aaa,aaaa, aaaaaaa
so what %20s will do is keep
!(consider this as the 20th position)
aaa
aaaa
aaaaaaa

%-10s--> 10 spaces from the backward similarly.
Hope this is suffice.
Regards.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!