
How to Build a Mortgage Calculator in Java (with Replit + GitHub)

Introduction
Calculating mortgage payments is one of the most common financial operations — yet understanding how it works under the hood can be enlightening. In this article, you’ll learn how to build a Mortgage Calculator using pure Java, complete with user input, validation, error handling, and formatted currency output.
This project is beginner-friendly and can be run locally or directly on Replit, a cloud-based IDE.
Prerequisites
To follow along, you should know:
- Basic Java syntax (if, while, Scanner, etc.)
- How to run Java code using javac and java
- Basic arithmetic and formula usage
The Mortgage Formula
We use the standard amortised loan formula:
M=P×r(1+r)n(1+r)n−1M = P \times \frac{r(1 + r)^n}{(1 + r)^n — 1}M=P×(1+r)n−1r(1+r)n
Where:
- M = Monthly payment
- P = Principal (loan amount)
- r = Monthly interest rate (annual rate / 12 / 100)
- n = Total number of payments (years * 12)
Project Structure
Here’s the layout:
MortgageCalculator/
├── src/
│ └── com/
│ └── kshitij/
│ ├── MortgageCalculator.java
│ └── MortgageCalculator_ErrorHandling.java
├── README.md
MortgageCalculator.java (Basic Version)
This version simply takes user input and calculates the mortgage:
public class MortgageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
System.out.print("Enter Principal: ");
int principal = scanner.nextInt();
System.out.print("Enter Annual Interest Rate: ");
double annualInterestRate = scanner.nextDouble();
System.out.print("Enter Period (Years): ");
double years = scanner.nextDouble();
double monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / PERCENT;
int numberOfPayments = (int) (years * MONTHS_IN_YEAR);
double mortgage = principal * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) /
(Math.pow(1 + monthlyInterestRate, numberOfPayments) - 1);
String formattedMortgage = NumberFormat.getCurrencyInstance(Locale.CANADA).format(mortgage);
System.out.println("Monthly Payment: " + formattedMortgage);
}
}
MortgageCalculator_ErrorHandling.java (Enhanced Version)
This version improves the input process with validation and error messages using try-catch and while loops.
public class MortgageCalculator_ErrorHandling {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
double principal = 0;
double annualInterestRate = 0;
double years = 0;
// Validate Principal
while (true) {
try {
System.out.print("Enter principal ($1K - $1M): ");
principal = scanner.nextDouble();
if (principal < 1000 || principal > 1_000_000) throw new InputMismatchException();
break;
} catch (InputMismatchException e) {
System.out.println(" Enter a number between 1,000 and 1,000,000");
scanner.nextLine();
}
}
// Validate Interest Rate
while (true) {
try {
System.out.print("Annual Interest Rate (0 - 30): ");
annualInterestRate = scanner.nextDouble();
if (annualInterestRate <= 0 || annualInterestRate > 30) throw new InputMismatchException();
break;
} catch (InputMismatchException e) {
System.out.println(" Enter a value greater than 0 and less than or equal to 30");
scanner.nextLine();
}
}
// Validate Period
while (true) {
try {
System.out.print("Period (Years): ");
years = scanner.nextDouble();
if (years <= 0 || years > 30) throw new InputMismatchException();
break;
} catch (InputMismatchException e) {
System.out.println(" Enter a value between 1 and 30");
scanner.nextLine();
}
}
double monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / PERCENT;
int numberOfPayments = (int) (years * MONTHS_IN_YEAR);
double mortgage = principal * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) /
(Math.pow(1 + monthlyInterestRate, numberOfPayments) - 1);
String formattedMortgage = NumberFormat.getCurrencyInstance(Locale.CANADA).format(mortgage);
System.out.println("\n Monthly Payment: " + formattedMortgage);
}
}
Try It Online (Replit)
If you don’t want to install anything, you can run both versions directly in your browser using Replit:
Run Mortgage Calculator on Replit
- Click Run to launch
- Enter values as prompted
- View output in the console
If you get a Main class not found error, ensure your .replit file contains:
run = "javac src/com/kshitij/MortgageCalculator.java && java -cp src com.kshitij.MortgageCalculator"
GitHub Source Code
All source code is open and available here:
GitHub Repo — MortgageCalculator
Includes:
- Clean code with input validation
- README instructions
- Two working calculator versions
- Compatible with Replit, IntelliJ, or terminal
References
This project uses the standard mortgage formula from WikiHow
Final Thoughts
This project is a great way to practice real-world Java concepts:
- User input and validation
- Loops and error handling
- Math formulas and formatting
- Modular project structure
If you’re learning Java and want a hands-on project with clear logic and utility — this is a great place to start.