Reputation: 1
[PLEASE READ] EDIT: The problem has been narrowed down to the fact that I am calculating the amount of payments necessary inaccurately. I am now in need of a way to calculate the number of payments required to pay off x amount of loan, with y amount interest, and z amount paid per month only. My initial approach was to take the monthly payment provided by the user (e.g. $250) with an interest rate (e.g. 13.5%) to pay off a loan (e.g. $1000) and get the amount paid per interested month (worded weirdly, I know) Thus you'd get:
250 - 250 * 0.135 = 216.25
Then divide the total loan by that number, so
1000 / 216.25 = 4.62
Then ceil the value to get 5 total payments. This worked for the example just given, as you can see in the code at the bottom of my post, but if we take for example:
$200,000 loan, 6% interest rate, and payments of $1500 per month, we get:
1500 - 1500 * 0.06 = 1410
And then:
$200,000 / 1410 = 141.8 ish
Thus the amount of payments (according to this calculation) should be 142. However, that is drastically incorrect, as you can see in the comments thread. Through trial and error, the correct number of payments is actually 221, and I haven't been able to attain this value using any calculations so far. Hence the following:
tl;dr - I need a way to accurately calculate the number of payments in an amortization using only the initial loan amount, the interest rate, and the proposed amount paid per month.
Thanks in advance, and this def narrows down the problem.
[ORIGINAL POST] I've created a program that does amortization charts for my AP Compsci Class. Everything was running smoothly until I tell the program to handle bigger numbers. For some reason, the program just halts at a certain value and I can't figure out why. I was hoping someone on here could help me debug it.
I use intelliJ for my IDE, and I don't know what other info would really help here.
First of all: Yes, I know I should use ArrayList instead of a 1d array, I started this before we learned about them in class and I don't feel like redoing it. I just want to know the problems with my existing code so I can hand it in and be done with it. The only requirement is for it to function properly. Yes, I know the code utterly sucks, I don't want to hear it please. Just looking for the problem with my existing code. Yes, I know I suck at coding. Please don't roast ;-;
Tried commenting out the loop, literally calculating it by hand, going through step by step, etc. Can't seem to draw any conclusion.
Here is the method:
public static void amortization(){
int[] info = new int[10000000];
double Lamt;
double IRate;
double MRate;
double payMonth;
double firstlyMonth;
double interestedSub;
double interestedAmt;
double amtPayments;
double mPayment;
double iPayment;
double pPayment;
double pBalance;
double q;
double finalMPymt;
double finalIPymt;
double finalPPymt;
double finalPBalance;
Scanner amortkey = new Scanner(System.in);
System.out.println("Please enter the following values:");
System.out.println("Loan amount:");
Lamt=amortkey.nextDouble();
System.out.println("Annual Interest Rate, in decimal form:");
IRate=amortkey.nextDouble();
System.out.println("Monthly Payment Amount:");
payMonth=amortkey.nextDouble();
interestedSub=payMonth * IRate;
interestedAmt=payMonth - interestedSub;
firstlyMonth=Lamt / interestedAmt;
amtPayments=Math.ceil(firstlyMonth);
MRate=IRate / 12;
for(int a=0; a<amtPayments; a++){
info[a]=a+1;
}
System.out.println();
System.out.println(" PAYMENT MONTHLY INTEREST PRINCIPAL PRINCIPAL");
System.out.println(" NUMBER PAYMENT PAYMENT PAYMENT BALANCE");
System.out.println();
pBalance=Lamt; //1000
//THIS LOOP DETERMINES THE CHART
for(int b=0; b<amtPayments-1; b++){//b starts 0, must climb to the payments that must be made.
iPayment=pBalance * MRate; //calculate interest payment by taking the rate portion of the principal away.
pPayment=payMonth - iPayment;//calculate principal payment independently from total month pay
q=pBalance - pPayment;//calculate new balance of principal, q
mPayment=iPayment + pPayment;//make monthly payment equal to whatever the principal is plus the interest owed
pBalance=q;//reset the loop with a new principal balance
System.out.println(" " + info[b] + " " + Math.round(mPayment * 100.00) / 100.00 + " " + Math.round(iPayment * 100.00) / 100.00 + " " + Math.round(pPayment * 100.00) / 100.00 + " " + Math.round(pBalance * 100.00) / 100.00);
System.out.println();
if(pBalance<mPayment){
finalIPymt=pBalance * MRate;
finalMPymt=pBalance + finalIPymt;
finalPPymt=pBalance;
finalPBalance=0;
System.out.println(" " + info[b+1] + " " + Math.round(finalMPymt * 100.00) / 100.00 + " " + Math.round(finalIPymt * 100.00) / 100.00 + " " + Math.round(finalPPymt * 100.00) / 100.00 + " " + Math.round(finalPBalance * 100.00) / 100.00);
}
}
}
First try the numbers 1000, 0.135, and 250.
The expected output is
PAYMENT MONTHLY INTEREST PRINCIPAL PRINCIPAL
NUMBER PAYMENT PAYMENT PAYMENT BALANCE
1 250.0 11.25 238.75 761.25
2 250.0 8.56 241.44 519.81
3 250.0 5.85 244.15 275.66
4 250.0 3.1 246.9 28.76
5 29.09 0.32 28.76 0.0
And the actual result is indeed the same thing. It works like a charm.
Now try these numbers: 200000, 0.06, and 1500.
For some odd reason, the program ends at entry 141 with a final output of:
141 1500.0 494.88 1005.12 97970.54
It ends with exit code 0, no errors, and just simply stops right there.
I know it's a lot to ask for, but I'd appreciate it if someone put the code into their IDE and tinkered with it until they found the problem. Please try the exact numbers I put in this post and see if anything is off, I want to be able to compare the problem to the solution. Apologies again for the messy and amateur code, I'm relatively new to all this still. I appreciate any help, and thanks in advance.
Cheers :D
Upvotes: 0
Views: 52
Reputation: 73
Increment b and then use that b instead of b+1, before you write this line
System.out.println(" " + info[b+1] + " " + Math.round(finalMPymt * 100.00) / 100.00 + " " + Math.round(finalIPymt * 100.00) / 100.00 + " " + Math.round(finalPPymt * 100.00) / 100.00 + " " + Math.round(finalPBalance * 100.00) / 100.00);
Upvotes: -2