Results 1 to 9 of 9
Thread: for statement
 10182011, 01:36 AM #1Member
 Join Date
 Oct 2011
 Posts
 5
 Rep Power
 0
for statement
I am really new to Java (and programming altogether, and forums as a matter of fact), and I am trying to create a mortgage calculator that will calculate the monthly payment (and then break down all of the payments for the term of the loan by interest and principal) using an array. I
am using a for loop with a nested do while loop. Everything seems to work fine for the first two loans, but the third is giving me fits! How can a loop run correctly twice and then act
differently all of the sudden?
Code:
Java Code:package mortgagecalculator; import java.math.*; public class NewClass { public static void main(String[] args) { //Declaring this class's fields int purchasePrice; double interestRate; double monthlyInterest; double newInterest; double monthlyPrincipal; double newPrincipal; double totalMonthly; double totalCost; int mortgageTerm ; double A; double B; double C; int loan; int newMonth; int month = 1; double newTotalCost; purchasePrice = 200000; int [] loanNumber = {1, 2, 3}; double[] interestArray={0.0535, 0.055, 0.0575}; int [] termArray = {7, 15, 30}; try{ for(int i=0; i<=loanNumber.length; i++){ totalMonthly=0; mortgageTerm=0; monthlyPrincipal=0; monthlyInterest=0; mortgageTerm = (termArray[i])*12; monthlyPrincipal = purchasePrice/mortgageTerm; monthlyInterest = (interestArray[i])/12; A = (monthlyInterest*Math.pow(1+monthlyInterest, mortgageTerm)); B = (Math.pow(1+monthlyInterest, mortgageTerm)1); C = purchasePrice*(A/B); totalMonthly = C; totalMonthly = Math.round (totalMonthly*100); totalMonthly = totalMonthly/100; loan = loanNumber[i]; totalCost = totalMonthly*mortgageTerm; System.out.println(""); //Blank space for neatness System.out.println("***********************************************************"); //Border System.out.println("Loan #" + (loan) + " reflects a mortgage with a " + (termArray[i]) + " year term at " + (interestArray[i]) + ":"); System.out.println("Loan #" + (loan) + "'s payment will be " + (totalMonthly)); System.out.println("Loan #" + (loan) + "'s total payments made will equal to: " + (totalCost)); System.out.println("***********************************************************"); //Border System.out.println(""); //Blank space for neatness month = 1; newTotalCost = totalCost; do { //Begins my do while statement mortgageTerm; newTotalCost = newTotalCosttotalMonthly; newInterest = (newTotalCost*((monthlyInterest*12)/12)); newInterest = Math.round(newInterest * 100); newInterest = newInterest/100; newPrincipal = totalMonthlynewInterest; newPrincipal = Math.round(newPrincipal * 100); newPrincipal = newPrincipal/100; newMonth = month++; System.out.println("For loan #" + (loan) + ": Month " + newMonth + "'s Balance: " + newTotalCost + " Interest Due: " + newInterest + " Principal Paid: " + newPrincipal); } while(newInterest >= 0 && newPrincipal <= totalCost && newPrincipal >= 0 ); } } catch(Exception ex){ System.out.println(""); (in this case, nothing) } }
I know this is probably jumbled up, and there are better ways to do what I am trying to
do, but I am concerned with my loop results for now. Any help appreciated, THANKS!!Last edited by Fubarable; 10182011 at 01:53 AM. Reason: code tags added

Re: for statement
Please provide some details: how is it not working? What misbehavior are you seeing?
 10182011, 01:48 AM #3Member
 Join Date
 Oct 2011
 Posts
 5
 Rep Power
 0
Re: for statement
First two loans in the loop result in this way:
************************************************** *********
Loan #1 reflects a mortgage with a 7 year term at 0.0535:
Loan #1's payment will be 2859.79
Loan #1's total payments made will equal to: 240222.36
************************************************** *********
For loan #1: Month 1's Balance: 237362.56999999998 Interest Due: 1058.24 Principal Paid: 1801.55
For loan #1: Month 2's Balance: 234502.77999999997 Interest Due: 1045.49 Principal Paid: 1814.3
For loan #1: Month 3's Balance: 231642.98999999996 Interest Due: 1032.74 Principal Paid: 1827.05
For loan #1: Month 4's Balance: 228783.19999999995 Interest Due: 1019.99 Principal Paid: 1839.8
For loan #1: Month 5's Balance: 225923.40999999995 Interest Due: 1007.24 Principal Paid: 1852.55
For loan #1: Month 6's Balance: 223063.61999999994 Interest Due: 994.49 Principal Paid: 1865.3
For loan #1: Month 7's Balance: 220203.82999999993 Interest Due: 981.74 Principal Paid: 1878.05
For loan #1: Month 8's Balance: 217344.03999999992 Interest Due: 968.99 Principal Paid: 1890.8
...etc
However, the third loan in the loop results this way:
************************************************** *********
Loan #3 reflects a mortgage with a 30 year term at 0.0575:
Loan #3's payment will be 1167.15
Loan #3's total payments made will equal to: 420174.00000000006
************************************************** *********
For loan #3: Month 1's Balance: 419006.85000000003 Interest Due: 2007.74 Principal Paid: 840.59
...and thats it!! Not sure what wrong.

Re: for statement
I'm no mortgage wizard, but shouldn't the loan calculations continue until the balance due is 0 or below? In other words, why all the conditions in the while boolean? why not just while the newPrinciple > 0?
 10182011, 02:30 AM #5Member
 Join Date
 Oct 2011
 Posts
 5
 Rep Power
 0
Re: for statement
You are right, I tried that already, and then I threw a slew of other conditions at it when it didn't work. Alone, newPrincipal > 0 threw me into an infinite loop on the first loan, however. Thanks for the tip though.

Re: for statement
Have you double checked your math then? I can't help you with the formulas, but I do know that there are mortgage calculators available online, or one can be whipped up in seconds using Excel or any other halfway decent spreadsheet.
 10182011, 02:42 AM #7Member
 Join Date
 Oct 2011
 Posts
 5
 Rep Power
 0
Re: for statement
Yes, I used a mortgage formula to get my calculations. In fact, I built this step wise, and until now all has worked out. Putting it all together... another story. Here is my confusion:
The loop works perfectly on the first two sets of array instances, but acts crazy on the final one. How is it possible for the loop to run as intended, and then act unexpectedly. I thought it does the same thing over and over until conditions are are thrown??

Re: for statement
When I run into situations like this, I pepper my code with println statements to check the state of various variables at various points in my program. i suggest you do the same. Starting with trying to find out why the first principle paid is a negative number.
 10182011, 02:48 AM #9Member
 Join Date
 Oct 2011
 Posts
 5
 Rep Power
 0
Similar Threads

if statement help
By Mrlowrider in forum New To JavaReplies: 1Last Post: 04202011, 07:14 PM 
If Statement in SQL
By Steffi1013 in forum JDBCReplies: 6Last Post: 04102010, 03:19 PM 
need help in sql statement
By chyeeqi in forum JDBCReplies: 6Last Post: 03282010, 07:49 PM 
if statement help please!!
By soc86 in forum New To JavaReplies: 5Last Post: 12022008, 02:56 PM 
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08012007, 04:45 PM
Bookmarks