This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Aligning columns in a text file with Java Formatter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Aligning columns in a text file with Java Formatter()" Watch "Aligning columns in a text file with Java Formatter()" New topic
Author

Aligning columns in a text file with Java Formatter()

Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
Hi,

I have been pulling my hair out trying to find information on how to align the columns in a text file I am writing with Java's Formatter (). I have a simple GUI application that I am writing to manage my personal budget. This is not for a class. Here is the code I have:

/code

for ( int i = 0; i < 24; i++)
{
output.format("%s\t",labels[i].getText());
output.format("\t");
output.format("%s\r\n\r\n",balance[i].getText());
}//end for
output.close();

//code

The output into the text file looks like this:

1. Savings 0

2. Rent 0

3. Electricity 123.0

4. Cable 0

5. Insurance 0

6. Sitters 0

7. Phone 0

8. Credit Card 123.0

9. Car 1 0

10. Car Repairs 123.0

11. Gas 0

12. Groceries 0

13. Medical Expenses 123.0

14. Entertainment 0

15. Miscellaneous 0

16. Benefits 0

17. Medicare 0

18. Social Security 0

19. FICA 0

20. Income Taxes 0

21. Income 500.0

22. Taxes Total 0

23. Expenses 492.0

24. Net Income 8.0


Does anyone know how to justify the second column with all the numbers? I tried setting the spacing with "%10s" and so on but it still messed it up. I was hoping that Formatter () had a method that would justify those columns, but I did not see one on suns web site.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
What about setting widths for the columns? If %s gives a String the same length as its input, %-20s gives a String left-aligned in a 20-character space. If %f gives a floating point number, then %7.2f gives up to "1234.56", so 1.23 would appear as "   1.23".
More details in the Formatter class, in the Java™ Tutorials section (quite brief) or in many of the books. I have Deitel and Deitel Java How to Program 6/e (2005) and it's pages 1326-1349.

And welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
If you use %10s that will only work if all your inputs are <= 10 characters. If you have 11 characters, then they will overflow the space and mess up the layout, rather than print an incomplete output.
Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
okay, so I was looking at that link you posted in the Java Tutorials section and it looks like I could use a flag? argument of "_" to make my columns left-justified, if I am reading this right.
My question is, where would I put that flag in the arguments? Would it be:

Formatter output = new Formatter( _ );

That does not seem right.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Not _ but - (if I remember correctly).By the way, don't use \n or \r in printf, format, etc. Use %n.
Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
By the way, don't use \n or \r in printf, format, etc. Use %n

I am assuming that %n is the same as \n? So therefore %r should be the same as \r? What is the difference between \n \r and %n %r?

Btw, thanks for the link to the java tutorials. I almost have my problem figured out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Timothy Hoyle wrote: I am assuming that %n is the same as \n? . . .
No, it isn't, and %r has something to do with printing time in the 12-hour clock.

Did you see what %n gives you in the Formatter documentation (use ctrl-F 'n') or the Java™ Tutorials? The result varies from OS to OS: there are actually three common possibilities
  • DOS/Windows = \r\n
  • Older Macs = \r
  • Unix, solaris, Linux and newer Macs = \n


  • The character shown as \n is actually (char)0x0a which is the LINEFEED character often loosely called newline.
    The character shown as \r is the CR character which is (char)0x0d and stands for Campbell Ritchie carriage return.

    Your operating system and applications can covert one sort of line end to another in 99% of instances . . .

    Btw, thanks for the link . . .
    You're welcome and well done sorting it out.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Aligning columns in a text file with Java Formatter()