# Mortgage calculator problem

**URL:** <https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884>\
**Category:** Java\
**Created:** [July 23, 2022, 7:25pm UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884 "2022-07-23T19:25:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leela](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Leela](https://forum.codewithmosh.com/u/Leela)\
**Post date:** [July 23, 2022, 7:25pm UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/1 "2022-07-23T19:25:09Z")

</div>

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..) 😭

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,**

```auto
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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 23, 2022, 8:07pm UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/2 "2022-07-23T20:07:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Leela](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Leela](https://forum.codewithmosh.com/u/Leela)\
**Post date:** [July 24, 2022, 9:02am UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/3 "2022-07-24T09:02:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 24, 2022, 3:00pm UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/4 "2022-07-24T15:00:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![roxcntrum](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@roxcntrum](https://forum.codewithmosh.com/u/roxcntrum)\
**Post date:** [January 31, 2024, 10:23am UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/5 "2024-01-31T10:23:37Z")

</div>

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](https://cardiffmoneyman.com/mortgage-broker-newport/). They’re pros at navigating numbers!

---

<div class="post-metadata">

**Author:** ![jimin23](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jimin23](https://forum.codewithmosh.com/u/jimin23)\
**Post date:** [June 25, 2024, 6:14am UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/6 "2024-06-25T06:14:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josef23](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josef23](https://forum.codewithmosh.com/u/josef23)\
**Post date:** [June 27, 2024, 12:16am UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/7 "2024-06-27T00:16:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![a.madhu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a.madhu/32/6830_2.png) [@a.madhu](https://forum.codewithmosh.com/u/a.madhu)\
**Post date:** [July 11, 2024, 10:14pm UTC](https://forum.codewithmosh.com/t/mortgage-calculator-problem/13884/8 "2024-07-11T22:14:51Z")

</div>

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

 ![Screenshot (1682)](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/0/0300b0c468317b5edb44942866495fcf3ac153d5.png)  
after changes  
 ![Screenshot (1683)](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/6/6290ae72e361eaea0186474524c311ebda9fbd9c.png)  
we get a right result.
