File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt in String formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in String formatting" Watch "Doubt in String formatting" New topic
Author

Doubt in String formatting

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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

Joined: Jan 28, 2008
Posts: 5575

---------------------------
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

Joined: Jan 01, 2007
Posts: 333
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

Joined: Oct 06, 2008
Posts: 338
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

Joined: Oct 06, 2008
Posts: 338
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

Joined: Oct 06, 2008
Posts: 338
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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

Joined: Oct 06, 2008
Posts: 338
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

Joined: Oct 06, 2008
Posts: 338
How to use the String.format method in achieving the same output?
Regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
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

Joined: Oct 13, 2005
Posts: 36498
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in String formatting
 
Similar Threads
nullpointerexception with String???
setMinimumIntegerDigits problem
String
align output with StringBuffer
Display a integer always in two digit