• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I right align output for amortization

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a newbie to java programming, although I can figure out most of what I want in the program, I can't seem to get the right alignment for my output.
The requirements for my project have already been met, but to make the program aesthetically pleasing I would like my ouput right aligned.
Any help or suggestions would be greatly appreciated

//loop for amortization schedule
double E_Balance = userPrincipal;
double mInterest;
double mPrincipal;

for(int j = 0; j < userTerms; j++)
{

mInterest = (E_Balance * userInterest); //Interest paid per month
mPrincipal = (Mpayment - mInterest);// principal paid per month
E_Balance = (E_Balance - mPrincipal); //ending balance for each month after payment.


//print structure for amortization schedule
txtResults.append((j+1) + "\t\t" + dec.format(mInterest)
+ "\t\t" + dec.format(E_Balance) +"\n");


This is just a piece of my code.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the String.format() method and the associated Formatter class. You can create strings with format() by using a format string to specify what you want to happen, something like:

String outputString = String.format("%10s %5.2", testString, total);

to produce a string that has 10 spaces for the second (string) parameter and a right-aligned 5 spaces with 2 decimals for a float var of total.

I may have the specs wrong; right-alignment may require a negative sign or something.

rc
 
lyn flick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks RC I'll give it a try!
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mInterest = (E_Balance * userInterest); //Interest paid per month
mPrincipal = (Mpayment - mInterest);// principal paid per month
E_Balance = (E_Balance - mPrincipal); //ending balance for each month after payment.


//print structure for amortization schedule
txtResults.append((j+1) + "\t\t" + dec.format(mInterest)
+ "\t\t" + dec.format(E_Balance) +"\n");



txtResults.append( String.format( "%4d %8.2f %8.2f %8.2f\n", j+1, mInterest, mPrincipal, E_Balance ) );
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope it's not too late, but . . . welcome to the Ranch
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need a monospaced font like Courier, or an equal number of characters still won't mean right aligned. In a non-monospaced font like Arial, a "w" is wider than an "i", and you'll see this in your alignment. In a monospaced font all characters are equally wide.

To create a monospaced font you can use the following code:

 
reply
    Bookmark Topic Watch Topic
  • New Topic