File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Help with Formatters ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Formatters !" Watch "Help with Formatters !" New topic

Help with Formatters !

richard, robinson

Joined: Jul 30, 2001
Posts: 3
I am producing a report showing what values have been updated/rejected and batch totals for end user. My problem(s) are that I am unsure of how to get the data formatted and presentable for the users!.
As you can see from the example, it looks pretty naff, anyway that I can format it! to left align the string values?.
I am new to Java, but have basic understanding of what I am doing. Help
Example of the problem :
Date Account Amount Receipt No.
23/07/01 123456 1.00 1
23/07/01 123456 10.00 2
23/07/01 123456 100.00 3
23/07/01 123456 1000.00 4

Java, its just a cup thing !
Lucas Richardson
Ranch Hand

Joined: Jul 08, 2001
Posts: 32
The quickest and easiest thing to do would be to use tabs (put a /t in the string where you want one), as long as you don't mind the somewhat large gaps it may leave.
Otherwise, depending on how your code is, and assuming that the $ amount is the only thing that will change in length (besides the last collumn), you could make that a String before displaying it, get it's length, and then pad spaces based on that.
richard, robinson

Joined: Jul 30, 2001
Posts: 3
I will try that, I am used to other languages where you can format output lines somewhat easier!.
pity there is no facility to left align strings/force strings to be fixed length, would be easier.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
So just create a method that takes a String, length and justify (right or left) and adds spaces where you want. Use a fixed width font for your display and you've got it. For numbers use NumberFormat.
[This message has been edited by Paul Stevens (edited July 30, 2001).]
richard, robinson

Joined: Jul 30, 2001
Posts: 3
Yes I have managed to work it out with the help of BB members, Thankyou.
My Code !!!
// Format transaction count for report from Data File
tempInt = Integer.valueOf(line.substring(1,9));
noofTransactions = tempInt.intValue();
tempString2 = String.valueOf(noofTransactions);
theLength = ( 5 - tempString2.length());
for (int i =0; i < theLength; i ++)
tempString2 = " " + tempString2;
I agree. Here's the link:
subject: Help with Formatters !