views:

117

answers:

7

Hi. I'm trying to find a complete tutorial about formatting strings in java.

I need to create a receipt, like this:

       HEADER IN MIDDLE
''''''''''''''''''''''''''''''
Item1                   Price
Item2 x 5               Price
Item3 that has a very
long name....           Price
''''''''''''''''''''''''''''''
Netprice:                 xxx
Grossprice:               xxx
VAT:                      xxx
Shipping cost:            xxx
Total:                    xxx
''''''''''''''''''''''''''''''
      FOOTER IN MIDDLE
A: 

formating string is some what complicated, for this kind of requirement. so its better to go for some reporting tool using the format you have given. which would be the better approach.

Either a crystal report or some others which are easy to implement.

solairaja
never heard of such tools, u have any link?
Johannes
r u using web or windows application in java
solairaja
i am using windows
Johannes
http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.businessobjects.integration.eclipse.devtools.doc/developer/Walkthrough9.html
solairaja
+3  A: 

The format to pass to string.format is documented here:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

From the page:

The format specifiers for general, character, and numeric types have the following syntax:

   %[argument_index$][flags][width][.precision]conversion

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.

The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

Espo
A: 

Trying to do this with formatting a string will cost you to much time and nerves. I would suggest a templating engine like Stringtemplate or something similar.

with doing these you will separate the presentation from the data and that will be a very good thing in the long run.

Dejan
A: 

See if these classes in java.text package can help..

Format

MessageFormat

Vijay Dev
A: 

Yea as solairaja said if you are planning to create reports or receipts you can go for reporting tools as Crystal reports

Crystal Report Crystal Report Tutorial

Or if you plan to use StringFormatting itself then "StringBuffer" would be the best option coz u can play around with it.

Richie
A: 

You should probably look at Java templating tools for this sort of multi-line reporting formatting.

Velocity is simple and forgiving of errors. Freemarker is very powerful but more intolerant. I would perhaps look at Velocity initially, and if you have to do more of this sort of work, take a further look at Freemarker.

Brian Agnew
A: 

Looks like the general advice from the community as a better approach to solve your problem is using a reporting tool.

Here you have a detailed list of open source Java charting and reporting tools: http://java-source.net/open-source/charting-and-reporting

The most well known is, in my opinion, Jasper Reports. A lot of resources about it are available on the web

JuanZe