Results 1 to 3 of 3
Thread: NaN output?
- 01-06-2010, 06:50 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
NaN output?
output:Java 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"); } } }
why is the output NaN? and what does "NaN" means? :confused: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
- 01-06-2010, 07:25 AM #2
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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:
- 01-06-2010, 09:32 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Here's where you hit your problem:
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).Java Code:monthlyIntRate = (annualIntRate / MONTHS_IN_YEAR) / 100;
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.
Similar Threads
-
Need help: output is not what i want
By Joshsmith in forum New To JavaReplies: 8Last Post: 09-28-2009, 10:09 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
how to c the output
By pro85 in forum Java AppletsReplies: 1Last Post: 02-11-2009, 09:09 AM -
Output
By Twiggy in forum New To JavaReplies: 14Last Post: 12-31-2008, 10:03 AM -
Why the output is always zero
By mehrotra.chitij in forum New To JavaReplies: 12Last Post: 04-25-2008, 04:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks