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 ]
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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);
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
I still think String.format might serve you well.
Ellen fish
Greenhorn
Joined: Oct 28, 2008
Posts: 27
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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).
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
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
Joined: Oct 06, 2008
Posts: 338
posted
0
Any replies?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
You have asked a new question, so please start a new thread.
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
posted
0
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.