Hi all,
I am having a problem with my code,
It is supposed to display 10 lines of text when I run it, but at first it does that, then it goes and displays 20 lines at a time. I am at my wits end with this one.
Can anybody help??
Here is my code
/*Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans: - 7 year at 5.35% - 15 year at 5.5% - 30 year at 5.75% Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.*/ import java.text.*; import java.lang.*; import java.io.*; public class Mortgage { public static void main(String[] args) { double rate[] = new double[] {.0535, .055, .0575}; double loan = 200000; int term[] = new int[] {7, 15, 30}; double payment; double balance; double interest; double principal; int month; int i = 0; int k = 0; int l = 0; /*Declaration of decimal formatting*/ DecimalFormat Currency = new DecimalFormat ("0.00"); BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); /*Monthly Mortgage Calculator*/ try { System.out.println("Firstly, we will display the payment, balance and interest for a "); System.out.println("\n$"+Currency.format(loan)+" over "+term[0]+" years at %"+(rate[0]*100)); System.out.println("\nMonth\t\tPayment\t\t Balance\t\t Interest\n"); principal = loan; while(i<term[0]*12) { for(int j = 0; j<10; j++) { payment = loan*((rate[0]/12)*Math.pow((1+(rate[0]/12)),(term[0]*12)))/(Math.pow((1+(rate[0]/12)),(term[0]*12))-1); month = i+1; interest = principal*rate[0]/12; balance = principal + interest - payment; System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest)); principal = balance; i++; if (i==term[0]*12) break; } buffer.read(); } System.out.println("Press the ENTER key to view next payments...\n"); } catch(IOException e) { System.out.println("Error: "+e); } try { System.out.println("Next, we will display the payment, balance and interest for a "); System.out.println("\n$"+Currency.format(loan)+" over "+term[1]+" years at %"+(rate[1]*100)); System.out.println("\nMonth\t\tPayment\t\t Balance\t\t Interest\n"); principal = loan; while(k<term[1]*12) { for(int j = 0; j<10; j++) { payment = loan*((rate[1]/12)*Math.pow((1+(rate[1]/12)),(term[1]*12)))/(Math.pow((1+(rate[1]/12)),(term[1]*12))-1); month = k+1; interest = principal*rate[1]/12; balance = principal + interest - payment; System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest)); principal = balance; k++; if (k==term[1]*12) break; } buffer.read(); } System.out.println("Press the ENTER key to view next payments...\n"); } catch(IOException e) { System.out.println("Error: "+e); } try { System.out.println("Lastly, we will display the payment, balance and interest for a "); System.out.println("\n$"+Currency.format(loan)+" over "+term[2]+" years at %"+(rate[2]*100)); System.out.println("\nMonth\t\tPayment\t\t Balance\t\t Interest\n"); principal = loan; while(l<term[2]*12) { for(int j = 0; j<10; j++) { payment = loan*((rate[2]/12)*Math.pow((1+(rate[2]/12)),(term[2]*12)))/(Math.pow((1+(rate[2]/12)),(term[2]*12))-1); month = l+1; interest = principal*rate[2]/12; balance = principal + interest - payment; System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest)); principal = balance; l++; if (l==term[2]*12) break; } buffer.read(); } } catch(IOException e) { System.out.println("Error: "+e); } } }
Thanks in advance.