• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Simple loan program

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to java and creating a simple loan amortization table. The user inputs loan amount, APR, and monthly payment. I'm 99% done but I realized the program subtracts the final payment (even if it's less than the loan balance remaining). Here is the code in question:

And here are the final few lines of the ouput:

I realize the amount interest paid at the bottom is wrong. I'll figure that out later. Any help or hints would be greatly appreciated.
 
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Johnny,

welcome to the Ranch!

The final payment will be at most the monthly payment. For instance, say the monthly payment = 50 (MP), the interest rate (IR) = 0.02, and the balance one payment period before the end = 40.
The interest paid (IP) is indeed balance * IR = 40 * 0.02 = 0.80. The principalPaid (PP) is first MP -  IP = 50 - 0.80 = 49.20.
Since now the PP > balance, we must limit the PP to that balance. That means that PP will become equal to the balance (40), and the monthly payment for that final month will be equal to PP + IP.

So, what check do you have to do in your for loop, and what adjustments are eventually needed?
 
Johnny Doey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Johnny,

welcome to the Ranch!

The final payment will be at most the monthly payment. For instance, say the monthly payment = 50 (MP), the interest rate (IR) = 0.02, and the balance one payment period before the end = 40.
The interest paid (IP) is indeed balance * IR = 40 * 0.02 = 0.80. The principalPaid (PP) is first MP -  IP = 50 - 0.80 = 49.20.
Since now the PP > balance, we must limit the PP to that balance. That means that PP will become equal to the balance (40), and the monthly payment for that final month will be equal to PP + IP.

So, what check do you have to do in your for loop, and what adjustments are eventually needed?



The only check I have in my for loop is balance>0. I don't think I completely understand your explanation above. I've tried differing combinations of conditions in my for loop but nothing is working. I feel like it's on the tip of my tongue...
 
Piet Souris
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay.

Your for loop starts as follows:

Now, here you could check the principalPaid, like:
 
Marshal
Posts: 80227
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johnny Doey wrote:I'm new to java

Again, welcome to the Ranch

and creating a simple loan amortization table.

That is not the correct way to calculate amortisation. There is a simple formula which I can't remember, but you can doubtless find easily enough.

The user inputs loan amount, APR, and monthly payment. You are not asking for APR anyway.

You use the nominal interest rate, not the APR.

I'm 99% done but I realized the program subtracts the final payment (even if it's less than the loan balance remaining). Here is the code in question:
. . .

There are all sorts of style issues about that code:-
When you say interestPaid, do you mean that is how much the customer is actually paying, or how much interest is due at that moment? I think you are calculating the latter, in which case you shou‍ld change the name of the variable.
Only write \n if somebody has told you they require the LF character. Use System.out.printf and the %n tag instead.
Don't write such long lines. I shall go back to your original post and break the long lines so you can see the right way to do it. You would have done that better than me had you done it before posting.
You have got too much code in a monolithic block. You shou‍ld have at least three methods for that code, at least,
  • 1: One for the keyboard input.
  • 2: One for the calculations.
  • 3: One for the display.
  •  
    Johnny Doey
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell, thanks for the response. Couple of things:

    1. I understand it's not the textbook definition of amortization (due to user input) but I thought "loan payment table" might seem vague
    2. I also understand I'm using nominal interest rate in my formulas but I was referencing what the user inputs, which is APR

    The interestPaid I'm using calculates how much interest is due each month (referencing newBalance).

    Thanks for the tips on formatting. I didn't even know what Java was six weeks ago. Learn something new everyday.
     
    Johnny Doey
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:Okay.

    Your for loop starts as follows:

    Now, here you could check the principalPaid, like:



    Thanks for the reply Piet. I don't understand how comparing principalPaid and balance solves my problem of the final payment being $50.00 instead of $47.83. Don't worry about it though, I don't want to bother you any further with my lack of understanding. Again, thanks for the help. I'll come back here after I clean up my code and get it right.
     
    Piet Souris
    Bartender
    Posts: 5584
    213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Fair enough.

    And renember: you are not bothering us at all! We're here trying to help, so do not hesitate to ask whenever you need help.

    I once wrote a micro introduction to annuity loans: click here

    Success!
     
    Campbell Ritchie
    Marshal
    Posts: 80227
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Johnny Doey wrote:. . . I'm using nominal interest rate in my formulas but I was referencing what the user inputs, which is APR

    I thought nominal interest was the correct thing to use. But be sure to make your user prompts clear and unambiguous. You cannot divide APR by twelve and get the monthly interest. You do that to the nominal rate. If you want to convert APR to monthly rate you would have to take the twelfth root of something.
    ¹²√(1 + APR ÷ 100)
    or similar. At least that is what APR means where I am writing from.

    The interestPaid I'm using calculates how much interest is due each month . . .

    That isn't called interest paid; it is called interest receivable (at least on this side of the Pond) or interest due. Make sure the variable name reflects what it means.
     
    Piet Souris
    Bartender
    Posts: 5584
    213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have to disagree slightly.
    It is very common to calculate the montly interest rate by dividing the yearly rate by 12. Only for theoretical interest calculations one calculates the monthly interest rate by taking the 12th root. There is indeed a difference between the 'real yearly interest rate' and the 'apparent yearly interest rate'.

    And as for the naming: just before the client pays his monthly payment, he is indeed interest due, and the interest is receivable. But dt seconds after payment, the client has 'interest paid', and his 'balance', as OP names it, has decreased.


     
    Campbell Ritchie
    Marshal
    Posts: 80227
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:I have to disagree slightly.
    It is very common to calculate the montly interest rate by dividing the yearly rate by 12.

    These rules may vary from country to country. As I said, you take the nominal annual rate and divide it by twelve.

    Only for theoretical interest calculations one calculates the monthly interest rate by taking the 12th root. There is indeed a difference between the 'real yearly interest rate' and the 'apparent yearly interest rate'.

    This may vary from country to country but APR is what you call the real rate. As you said, you would never use that to calculate interest due; the APR can be calculated from monthly interest to the 12th power.

    And as for the naming: just before the client pays his monthly payment, he is indeed interest due, and the interest is receivable. But dt seconds after payment, the client has 'interest paid', and his 'balance', as OP names it, has decreased.

    In which case the name balance is also wrong. Once the interest has been paid, the interest due drops to 0 until the next month.
     
    Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic