Mortgage calculator not printing monthly mortgage payment

So I tried my hand at the mortgage calculator and got stumped. I downloaded the source code as shown below and it does not print the monthly mortgage payment.

package com.codewithmosh;

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

public class Main {

public static void main(String[] args) {
    final byte MONTHS_IN_YEAR = 12;
    final byte PERCENT = 100;

    Scanner scanner = new Scanner(System.in);

    System.out.println("Principal: ");
    int principal = scanner.nextInt();

    System.out.println("Annual Interest Rate: ");
    float annualInterest = scanner.nextFloat();
    float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;

    System.out.println("Period (Years): ");
    byte years = scanner.nextByte();
    int numberOfPayments = years * MONTHS_IN_YEAR;

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

    String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
    System.out.println("Mortgage: " + mortgageFormatted);
}

}

What am I missing? From what I can tell the last line should print the concatenated string. Is it because I’m using the most recent version of Java 16 and this program was written with version 12? I don’t understand why he would provide source code that doesn’t actually work. I compared it to the code in the video and everything looks the same. Any answers or insight would be greatly appreciated. Thanks.

Hi, I copied Your code,
and it worked. Tried on both IntelliJ IDE and Apache Netbeam (on this should be more accurate)
using JDK 16

1 Like