1
votes

The task is to create a loan calculator based on the user input of min and max years of loan payments, loan amount, and min and max % rate with incremented value given by user as well for rate and number of years. Desired output should look like this:

Principle: $275000.0 Years to repay: 10 Interest Monthly Rate Payment


6.25 3087.7 6.75 3157.66 7.25 3228.53

Principle: $275000.0 Years to repay: 15 Interest Monthly Rate Payment


6.25 2357.91 6.75 2433.5 7.25 2510.37

Principle: $275000.0 Years to repay: 20 Interest Monthly Rate Payment


6.25 2010.05 6.75 2091.0 7.25 2173.53

Please help me fix errors. Thanks!

public static void main(String[] args){
            Scanner console = new Scanner (System.in);              

            System.out.println("This program computes monthly " + "mortgage payments.");
            System.out.print("Enter the loan amount:   "); 
            double loan = console.nextDouble(); 

            System.out.print("Enter the starting number of years to repay the loan:   ");
            int startingYears = console.nextInt(); 

            System.out.print("Enter the ending number of years to repay the loan:   ");
            int endingYears = console.nextInt();

            System.out.print("Enter the years increment between tables:   ");
            int incrementYears = console.nextInt();

            System.out.print("Enter the starting loan yearly interest rate, %:   ");
            double startingRate = console.nextDouble(); 

            System.out.print("Enter the ending loan yearly interest rate, %:    ");
            double endingRate = console.nextDouble(); 

            System.out.print("Enter the increment interest rate, %:   ");
            double incrementRate = console.nextDouble();            
            System.out.println();               

            System.out.println("Principle: $" + (double) loan);
            System.out.printf("Years to repay: %d\n", startingYears);
            System.out.println("-------- -------");

            double payment;             
                System.out.println("Rate\tPayment");

            for (int j = startingYears; j <= endingYears; incrementYears++) {
            for (double i = startingRate; i <= endingRate; incrementRate++){



                int n = 12 * startingYears;
                    double c = startingRate / 12.0 / 100.0;
                payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);  

                System.out.println(i + " " + payment);
//              System.out.println(round2(startingRate) + "\t" + round2(payment));
                startingYears += incrementYears;
            }
            }


        }

        }
1
How do I increment both the counters?Julia1677

1 Answers

0
votes

for (int j = startingYears; j <= endingYears; incrementYears++) {

for (double i = startingRate; i <= endingRate; incrementRate++) {

You are never incrementing the values of your counters i.e. i and j and hence the values of i and j will always be equal to the initialized values and these loops will run for infinite times.

Increment both the counters to be able to reach the termination condition i.e. j <= endingYears and i <= endingRate respectively.

Here is the code snippet:

System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
    System.out.printf("Years to repay: %d\n", j);
    System.out.println("-------- -------");
    System.out.println("Rate\tPayment");
    for (double i = startingRate; i <= endingRate; i+=incrementRate){
        int n = 12 * j;
        double c = i / 12.0 / 100.0;
        double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1); 
        System.out.println(i + "\t" + payment);
    }
    System.out.println();
}