Results 1 to 3 of 3

Thread: NaN output?

  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default NaN 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");
            }
        }
    }
    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. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    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. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

Similar Threads

  1. Need help: output is not what i want
    By Joshsmith in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 11:09 AM
  2. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  3. how to c the output
    By pro85 in forum Java Applets
    Replies: 1
    Last Post: 02-11-2009, 10:09 AM
  4. Output
    By Twiggy in forum New To Java
    Replies: 14
    Last Post: 12-31-2008, 11:03 AM
  5. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 05:05 AM

Posting Permissions

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