Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Aligning columns in a text file with Java Formatter()

 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not _ but - (if I remember correctly).By the way, don't use \n or \r in printf, format, etc. Use %n.
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic