I am writing a code for class that calculates 3 different mortgage rates with 3 different time periods and then displays the monthly payment. What I am getting for the output is on 2 of the payments is a negative payment amount. I am at a loss to understand why. :sad:

import java.math.*; //*loan calculator
import java.text.*; //*formats numbers
public class MultipleMortgageLoans {

public static void main(String args[])

double mortgageCalculator1[] = new double[6];
double mortgageCalculator2[] = new double[6];
double mortgageCalculator3[] = new double[6];
double loan = 200000; // Declare the loan amount

double term1 =360; //The 30 year mortgage
double interestRate1 = 0.0575; //*5.75% 5.75/100interest rate 30 year mortgage
double monthlyRate1 = (interestRate1/12);
double term2 =84; //The 7year mortgage loan
double interestRate2 = 0.0535; //*5.35 5.35/100 interest rate 7 year mortgage
double monthlyRate2 = (interestRate1/12);
double term3 =180; //The 15 year mortgage
double interestRate3 = 0.0550; //*5.5 5.5/100 interest rate on 15 year mortgage
double monthlyRate3 = (interestRate1/12); //* monthlyRate for 15 year mortgage

//Discount factor calculations for the three loans
double discountFactor1 = (Math.pow((1 + monthlyRate1),term1)-1/(monthlyRate1 * Math.pow((1 + monthlyRate1),term1)));

double discountFactor2 = (Math.pow((1 + monthlyRate2),term2)-1/(monthlyRate2 * Math.pow((1 + monthlyRate2),term2)));
double discountFactor3 = (Math.pow((1 + monthlyRate3),term3)-1)/(monthlyRate3 * Math.pow((1 + monthlyRate3),term3));
double payment1 = loan/discountFactor1; //*Rate Calculation for 30 year mortgage
double payment2 = loan/discountFactor2; //*Rate Calculation for 7 year mortgage
double payment3 = loan/discountFactor3; //*Rate Calculation for 15 year mortgage

java.text.DecimalFormat dfm = new java.text.DecimalFormat(",###.00");
System.out.println("Your monthly payment is $" + dfm.format(payment1));
System.out.println("Your monthly payment is $" + dfm.format(payment2));
System.out.println("Your monthly payment is $" + dfm.format(payment3));

payment1 = loan/discountFactor1;//*Rate Calculation
payment2 = loan/discountFactor2;//*Rate Calculation
payment3 = loan/discountFactor3;//*Rate Calculation


Plz use code blocks

and please use real words, not things like "plz", "u", etc..

At first glance your problem has almost certainly to do with your mathematics.
You THINK you're doing floating point divisions and multiplications, but are you really?
Think about how Java treats the results of a division between 2 numbers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.