This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in String formatting

 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the output to be in this format:

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



How to get that desired output.
Regards.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------
Al is 45 years old
Al23455 is 45 years old
Al23455 is 459999 years old
-----------------------------

can you please explain me that what is the relation between lines?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried reading the java doc for String.format?

It works, i believe, by taking the first string, and scanning it for a format chactor, such as %s when it finds such a token it will replace that token by the second argument and move on to the next.

So in your code:

Ths first formatting token %s is replaced with Al, and the second formatting token %d is replaced by 45.

%s represents tokens to be replaced by String, while
%d represents tokens to be replaced by int

I believe format tokens goes back to the readln method and i think may have been borrowed from C.

G
[ December 09, 2008: Message edited by: Gavin Tranter ]
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear seetharaman venkatasamy,
Sorry I've written & used String.format to have my output in a formatted manner in the form:

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


This is a formatted output.
How to do that in Java?
I somebody could modify my code to work this way I'll be happy!
Regards
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the desired output should be in the form:

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


Regards
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The desired output whether I hard-code it or put it in quotes or codes is not getting displayed properly.
May be the page is deleting the spaces.

I want the output to be in aligned format

like

1 2 3
1 2 3

eventhough the length of rows may vary.
Regards.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the format specification you are using for your string.
Compare it with the examples from the message where you originally posted this question.

The problem is very obvious.
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use printstream's(out) printf method it's somewhat working.
But I need to manually calculate the whitespaces & issue printf accordingly with 8s , 9s ...
Is there any other way i.e method so that strings can be automatically aligned.





Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • 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


%-10s--> 10 spaces from the backward similarly.
Hope this is suffice.
Regards.

[edited by: Campbell Ritchie. Add code tags around the aaa aaaa to preserve formatting. This may or may not align properly, depending on the font on people's browsers. ]
[ December 10, 2008: Message edited by: Campbell Ritchie ]
 
ramya narayanan
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use the String.format method in achieving the same output?
Regards.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done getting it worked out . I think when I sent you and Ellen to the three format and one printf methods I gave all 4 links. If you read them, you find they use exactly the same parameters; if it works with one method, it will work with the same parameters with another.

In the case of String.format tryand see how it works.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please maintain indentation inside the code tags; it makes the posting easier to read.

I have added code tags around where you quoted your printout; without code tags the spaces vanish, so you don't see what actually happened.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic