Mortgate calculator miscalculation

Hello. I am a total newbie to Java (and serious programming). I thought I had written this program correctly, but I am getting different results than Mosh was getting with his sample input (Principal: 1,000,000; Interest rate: 3.92; Period: 30 years; Mortgage (calculated): $4728.13). When I enter the same values, I get $3265.67 for the monthly payments. Also, my “calculateBalance” function keeps returning $0, so it seems obvious that it’s a calculation error, but I cannot find it.


import java.text.NumberFormat;
import java.util.Scanner;

public class Main {
    private final static byte MONTHS = 12;
    private final static byte PERCENT = 100;

    public static void main(String[] args) {
        GetMortgage();
    }

    private static void GetMortgage() {
        /*
        Principal: 1,000,000
        Interest rate: 3.92
        Period: 30 years
        Mortgage: $4728.13
        */
        final double PRINCIPAL_MIN = 1_000;
        final double PRINCIPAL_MAX = 1_000_000;

        final double RATE_MIN = .01;
        final double RATE_MAX = 100.00;

        final byte YEARS_MIN = 1;
        final byte YEARS_MAX = 30;

        Scanner scan = new Scanner(System.in);
        double principal = getInput("Principal ($1K - $1M)", "Invalid entry. Please enter a value between $1K and $1M", scan, PRINCIPAL_MIN, PRINCIPAL_MAX);
        double rate = getInput("Interest rate (.01% - 100%)", "Invalid entry. Please enter a value between .01% and 100%", scan, RATE_MIN, RATE_MAX);
        int period = (int)(getInput("Period (years)", "Invalid entry. Please enter a value between 1 and 30", scan, YEARS_MIN, YEARS_MAX));
        scan.close();

        double mortgage = calculateMortgage(principal, rate, period);
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        printMortgage(mortgage, nf);
        printPaymentSchedule(principal, rate, period, nf);
    }

    private static double getInput(String prompt, String failMessage, Scanner sc, double min, double max) {
        System.out.print(prompt + ": ");
        while (true) {
            if (!sc.hasNextDouble()) {
                System.out.println(failMessage);
            } else {
                double input = sc.nextDouble();
                if (input >= min && input <= max) {
                    return input;
                }
                System.out.println(failMessage);
            }
            sc.nextLine();
        }
    }

    private static double calculateMortgage(double principal, double rate, int period) {
        rate = rate / PERCENT / MONTHS;
        period *= MONTHS;

        return principal
			* (rate * Math.pow((1 + rate), period))
			/ Math.pow(1 + rate, period) - 1;
    }

    private static double calculateBalance(double principal, double rate, int period, short numberOfPaymentsMade) {
        rate = rate / PERCENT / MONTHS;
        period *= MONTHS;

        double balance = principal
			* (Math.pow(1 + rate, numberOfPaymentsMade) - Math.pow(1 + rate, numberOfPaymentsMade))
			/ (Math.pow(1 + rate, numberOfPaymentsMade) - 1);

        return balance;
    }

    private static void printMortgage(double mortgage, NumberFormat nf) {
        String payments = nf.format(mortgage);
        System.out.println();
        System.out.println("MORTGAGE");
        printRepeatedChar("-", 25);
        System.out.println("Monthly payments: " + payments);
    }

    private static void printPaymentSchedule(double principal, double rate, int period, NumberFormat nf) {
        System.out.println();
        System.out.println("PAYMENT SCHEDULE");
        printRepeatedChar("-", 50);

        for(short month = 1; month < period * MONTHS; month++) {
            double balance = calculateBalance(principal, rate, period, month);
            String formattedBalance = nf.format(balance);
            System.out.println(formattedBalance);
        }
    }

    private static void printRepeatedChar(String s, int repeatCount) {
        System.out.println(s.repeat(repeatCount));
    }
}

here is the calculation error

Did you resolve this already? Your calculateBalance() has duplicate
operations (1 more than needed), and also you need to close your parenthesis in your calculateMortgage(). Here here is the open parenthesis in YOUR CODE:

image

Hello King_Sardius, I really want to learn java, I have watched many other courses of java on YouTube but am not able to understand them and feels like java is very complicated and i will not learn it but one day i watched Mosh beginner course of java on YouTube and am really impressed, the way Mosh explains, everything become easier to understand for me. But now problem for me is that Mosh full java mastery course is paid and here in my country dollar rate is too much high that’s why I am not able to pay😔, because of my wealth condition, I really want to learn java but for this purpose its not possible for me. I want your help, could you give me this java course? I promise I will pay you when I get job and also pay to Mosh when I will get job, I hope you understand my situation.

Check the interest rate conversion to a monthly rate in your UIF Calculator function. Ensure it divides the annual rate by 12 and converts it to decimal form (e.g., 3.92% should be 0.0392/12). Share your code for more help

The calculation might be correct, but the result seems off due to decimal precision. Consider using Java’s Decimal Format to format the output properly, or verify the result with a UIF calculator.