Mortgage calculator problem

Hello all, I am new to Java, can anyone help me look at the results?
I checked with Mosh video like 10 times, but after I run the calculator…

Results … (not ending with 0 or something else…but…) :sob:

Principal: 180000
Annual Interest Rate: 4
Period (Years): 10

MORTGAGE

Monthly Payments: $1,822.41

PAYMENT SCHEDULE

$30,501.37
$106,742.99
$132,143.35
$144,833.42
$152,439.37
$157,503.28
$161,114.59
$163,818.05
$165,916.29
$167,590.87
$168,957.36
$170,092.79
$171,050.48
$171,868.55
$172,574.92
$173,190.55
$173,731.46
$174,210.12
$174,636.37
$175,018.08
$175,361.62
$175,672.22
$175,954.17
$176,211.08
$176,445.95
$176,661.34
$176,859.43
$177,042.08
$177,210.89
$177,367.27
$177,512.43
$177,647.42
$177,773.19
$177,890.55
$178,000.24
$178,102.91
$178,199.13
$178,289.43
$178,374.26
$178,454.06
$178,529.19
$178,600.00
$178,666.80
$178,729.88
$178,789.48
$178,845.85
$178,899.19
$178,949.72
$178,997.60
$179,043.01
$179,086.09
$179,126.99
$179,165.85
$179,202.78
$179,237.89
$179,271.29
$179,303.09
$179,333.35
$179,362.18
$179,389.65
$179,415.84
$179,440.81
$179,464.62
$179,487.34
$179,509.03
$179,529.73
$179,549.50
$179,568.39
$179,586.43
$179,603.68
$179,620.16
$179,635.92
$179,650.99
$179,665.40
$179,679.19
$179,692.38
$179,705.01
$179,717.09
$179,728.66
$179,739.73
$179,750.34
$179,760.50
$179,770.23
$179,779.55
$179,788.48
$179,797.03
$179,805.23
$179,813.09
$179,820.62
$179,827.84
$179,834.77
$179,841.40
$179,847.77
$179,853.87
$179,859.72
$179,865.33
$179,870.72
$179,875.88
$179,880.84
$179,885.59
$179,890.15
$179,894.52
$179,898.72
$179,902.75
$179,906.61
$179,910.32
$179,913.88
$179,917.30
$179,920.58
$179,923.73
$179,926.75
$179,929.65
$179,932.44
$179,935.11
$179,937.67
$179,940.14
$179,942.50
$179,944.78
$179,946.96
$179,949.05

Process finished with exit code 0

Here is the project,

package com.olivia;

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

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

    public static void main(String[] args) {
        int principal = (int) readNumber("Principal: ", 1000, 1_000_000);
        float annualInterest = (float) readNumber("Annual Interest Rate: ", 1, 30);
        byte years = (byte) readNumber("Period (Years): ", 1, 30);

        printMortgage(principal, annualInterest, years);
        printPaymentSchedule(principal, annualInterest, years);
    }

    private static void printMortgage(int principal, float annualInterest, byte years) {
        double mortgage = calculateMortgage(principal, annualInterest, years);
        String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
        System.out.println();
        System.out.println("MORTGAGE");
        System.out.println("--------");
        System.out.println("Monthly Payments: " + mortgageFormatted);
    }

    private static void printPaymentSchedule(int principal, float annualInterest, byte years) {
        System.out.println();
        System.out.println("PAYMENT SCHEDULE");
        System.out.println("----------------");
        for (short month = 1; month <= years * MONTHS_IN_YEAR; month++) {
            double balance = calculateBalance(principal, annualInterest, (short) years, (byte) month);
            System.out.println(NumberFormat.getCurrencyInstance().format(balance));
        }
    }
    private static void getExtracted(double balance) {
        extracted(balance);
    }

    private static void extracted(double balance) {
        System.out.println(NumberFormat.getCurrencyInstance().format(balance));
    }

    public static double readNumber(String prompt, double min, double max) {
        Scanner scanner = new Scanner(System.in);
        double value;
        while (true) {
            System.out.print(prompt);
            value = scanner.nextFloat();
            if (value >= min && value <= max)
                break;
            System.out.println("Enter a value between " + min + " and " + max);
        }
        return value;
    }

    public static double calculateBalance(
            int principal,
            float annualInterest,
            short numberOfPaymentsMade,
            byte years) {

        float monthlyInterest = (annualInterest / PERCENT / MONTHS_IN_YEAR);
        int numberOfPayments = years * MONTHS_IN_YEAR;

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

        return balance;
    }

    public static double calculateMortgage(
            int principal,
            float annualInterest,
            byte years){

        float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
        int numberOfPayments = years * MONTHS_IN_YEAR;

            double mortgage = principal
                    * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
                    / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

            return mortgage;
    }
}

Given that the number is starting at approximately 0 and proceeding towards the principal ($180k), I’d say you probably inverted some of the math. I am also unsure why you are doing such a complicated calculation instead of just tracking the remaining balance as you go. I think you can just calculate the remaining balance with the monthly interest rate and accounting for the payment each month.

I am just following his video and start to learn java… not sure what happened///// :frowning_face:

This is not a math course, but I would carefully check the code in the calculateBalance method which seems to be where things are going wrong. If you are basically copying that code, I would guess the typo is in there.

It’s been a while but, i totally get the struggle when learning something new. Happy to help you troubleshoot your mortgage calculator. Can you share a bit more about the issue you’re facing? Maybe we can figure it out together.

Also, if you ever need assistance with a different kind of calculator, like for your home financing, you might want to chat with a Mortgage Broker in Newport. They’re pros at navigating numbers!

he calculation might be correct, but the result seems unexpected due to decimals. Use Java’s Decimal Format for formatting, or verify using a UIF calculator

he calculation might be correct, but the result seems unexpected due to decimals. Use Java’s Decimal Format for formatting, or verify using a UIF calculator

this is wrong programm. calculateBalance() parameters and arguments are different!
thats why you get a wrong result.


after changes

we get a right result.