My Mortgage Calculator Project - Version 1

package com.mortcalc;

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

public class Main {

    public static void main(String[] args) {

        /*
        mathematical formula to calculate mortgage

        M = [P × {r(1 + r)^n}/{(1 + r)^n - 1}]

        M = Mortgage
        P = Principal Amount
        r = Monthly Interest Rate
        n = Number of Monthly Payments [Periods (Months)]
        */

        // declaration of constant values
        final byte PERCENT = 100;
        final byte MONTHS_IN_YEAR = 12;

        Scanner userInput = new Scanner(System.in); // declaring scanner object to accept user input

        // asking for user input and giving back appropriate values
        System.out.print("Principal Amount: ");
        int principal = userInput.nextInt();

        System.out.print("Annual Interest Rate: ");
        float monthlyInterestRate = ((userInput.nextFloat() / PERCENT) / MONTHS_IN_YEAR);
        System.out.println("Monthly Interest Rate: " + monthlyInterestRate);

        System.out.print("Period (Years): ");
        short period= (short) (userInput.nextShort() * MONTHS_IN_YEAR); // stores 'period' in months
        System.out.println("Period (Months): " + period);

        userInput.close(); // closing the scanner object

        // formula for calculating mortgage implemented in java
        double mortgage = (principal * (monthlyInterestRate * (Math.pow(1 + monthlyInterestRate, period)))
                / ((Math.pow(1 + monthlyInterestRate, period)) - 1));

        // prints the properly formatted mortgage value in local currency
        System.out.println("Mortgage: " + NumberFormat.getCurrencyInstance().format(mortgage));


    }
}

what is your question?

I don’t have any questions. I was just sharing my take of the project with the community :slight_smile:

Hello
Please help me out this problem
The flouting numbers doesn’t work with my IntelliJ editor

Hello @Mnoo!
After taking a look at your screenshot, I think it’s because you’re separating the decimal value from the integral value (of annual interest rate) using a ‘comma’ instead of a ‘decimal’.

How you’ve written it - 3,92
How it should be written - 3.92

I hope that solves your issue. Cheers.

Actually I even tried (.)
but it doesn’t work either

that is great… keep going, bro.
.

1 Like

You did a very nice job. Great! I would definitely not be able to do something like this.

1 Like

Hi Mnoo

I’ve sent you my screenshot,

Good luck

I don’t know if this is fixed, but you may be dividing and multiplying with different “types” like int, float, double, long, etc. The problem is you’re receiving input of one type, and then making operations with different types. These operations are yielding a double, int, short, or long instead of a float as an output. Then you are trying to deposit the output into a float type variable “monthlyInterestRate” when in fact the output is not a float itself. So you get the exception/Error “InputMismatchException”. You are depositing a number that is not a float into a variable of type float. That is the issue with your code.