1. Member
Join Date
Dec 2009
Posts
68
Rep Power
0

## NaN output?

Java Code:
```public class LoanCalculator2 {

private static DecimalFormat df = new DecimalFormat(".00");

public static void main(String[] args) throws IOException {

final int MONTHS_IN_YEAR = 12;

double monthlyInterest,
monthlyIntRate,
monthlyPayment,

unpaidBal,
principal,
totalInterestToDate = 0.0;

int annualIntRate,loanAmount,
loanPeriod,
numberOfPayments;

System.out.print("Enter The Loan Amount: ");

System.out.print("Enter The Annual Interest Rate: ");

System.out.print("Enter The Loan Period (YEARS): ");

monthlyIntRate = (annualIntRate / MONTHS_IN_YEAR) / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment = (loanAmount * monthlyIntRate) / (1 - Math.pow (1 / (1 +
monthlyIntRate), numberOfPayments));
unpaidBal = loanAmount;

System.out.print("\n");
System.out.println("Loan Amount: " + "\$" + loanAmount);
System.out.println("Annual Interest Rate: " + annualIntRate);
System.out.println("Loan Period: " + loanPeriod + " Year");
System.out.println("Monthly Interest Rate: " + df.format(monthlyIntRate));
System.out.println("Number Of Payments: " + numberOfPayments);
System.out.println("Monthly Payment: " + df.format(monthlyPayment));

System.out.print("\n");
System.out.println("Payment" + "\t\t\t\t\t" + "Unpaid" + "\t" + "Total Interest");
System.out.println("  No." + "\t" + "Interest" + "\t" + "Principal" + "\t" + " Balance" + "\t" + "  to Date");
System.out.println("-----------------------------------------------------------------------------");
System.out.print("\n");

for (int paymentNo = 1; paymentNo <= numberOfPayments; paymentNo++) {

monthlyInterest = monthlyIntRate * unpaidBal;
principal = monthlyPayment - monthlyInterest;
unpaidBal = unpaidBal - principal;
totalInterestToDate = totalInterestToDate + monthlyInterest;

System.out.format("%4d %14.2f %16.2f %17.2f %16.2f", paymentNo, monthlyInterest, principal, unpaidBal, totalInterestToDate);
System.out.print("\n");
}
}
}```
output:
Java Code:
```Enter The Loan Amount: 5000
Enter The Annual Interest Rate: 12
Enter The Loan Period (YEARS): 1

Loan Amount: \$5000
Annual Interest Rate: 12
Loan Period: 1 Year
Monthly Interest Rate: .00
Number Of Payments: 12
Monthly Payment: �

Payment                                        Unpaid        Total Interest
No.        Interest        Principal         Balance          to Date
-----------------------------------------------------------------------------

1           0.00              NaN               NaN             0.00
2            NaN              NaN               NaN              NaN
3            NaN              NaN               NaN              NaN
4            NaN              NaN               NaN              NaN
5            NaN              NaN               NaN              NaN
6            NaN              NaN               NaN              NaN
7            NaN              NaN               NaN              NaN
8            NaN              NaN               NaN              NaN
9            NaN              NaN               NaN              NaN
10            NaN              NaN               NaN              NaN
11            NaN              NaN               NaN              NaN
12            NaN              NaN               NaN              NaN```
why is the output NaN? and what does "NaN" means? :confused:

2. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
8
When you divide by zero with double, take the square root of a negative number, overflow the maximum representable value etc. the result is a magic number called Double.NaN, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY. You check the result with:

3. Moderator
Join Date
Apr 2009
Posts
13,362
Rep Power
24
Here's where you hit your problem:

Java Code:
`monthlyIntRate = (annualIntRate / MONTHS_IN_YEAR) / 100;`
All the values on the right are ints, so this is doing integer division whereby fractions are ignored. This results in your monthly interest rate being zero, so you then get the NaN result from division by zero (as mentioned above).

Make your annual rate a double.

As a note, though you can probably ignore this as it's an exercise, but you shouldn't use floats/doubles for financial calculations. They'll go wrong eventually due to the fact floats are simply not accurate representations of decimal numbers.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•