-
NaN output?
Code:
public class LoanCalculator2 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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: ");
loanAmount = Integer.parseInt(br.readLine());
System.out.print("Enter The Annual Interest Rate: ");
annualIntRate = Integer.parseInt(br.readLine());
System.out.print("Enter The Loan Period (YEARS): ");
loanPeriod = Integer.parseInt(br.readLine());
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:
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:
-
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:
-
Here's where you hit your problem:
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.