Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:487)
at java.text.Format.format(Format.java:140)
at McVrideMortgageCalculator.Main.main(Main.java:69)
The above statement is the error that occurs when running code.

package McVrideMortgageCalculator;

import java.text.*;             //Allows text formatting and used for formatting currency in this program

public class Main {


    public static void main(String[] args) {

        double dblPrincipal = 200000;                  //principal amount
        double [] dblInterestRate = {5.35,5.5,5.75};     //intialized the array with values(interest rate)
        int []intLoanTermYear  = {7,15,30};              //intialized the array with values(loan term)
        double [] dblMonthlyPayment = {0,0,0};            //intialized the array with zero values(monthly payment)
        double [] dblMonthlyInterestRate= {0,0,0};        //intialized the array with zero values(monthly interest rate)
        int [] intLoanTermMonth = {0,0,0};               //intialized the array with zero values(loan term in months)
        double dblCurrentMonthlyInterest;
        double dblCurrentMonthlyPrincipalPayment;
        double dblLoanBalance = 0;

        for (int box = 0; box < dblInterestRate.length; box++)
        {       int intlinecounter = 1;
                int intTrackNum20Payment = 0;
            dblMonthlyInterestRate[box] = dblInterestRate[box]/(12 * 100); //calculate monthly interest rate per array

            intLoanTermMonth[box] = intLoanTermYear[box] * 12;             //calculate number of months of loan 

            /* Monthlypayment formula using array index values */
            dblMonthlyPayment[box] = dblPrincipal * ( dblMonthlyInterestRate[box] / (1 - (Math.pow((1 + dblMonthlyInterestRate[box]),-(intLoanTermMonth[box])))));

            //Print out Monthly Payment Amount
            NumberFormat CurrencyFormat = NumberFormat.getCurrencyInstance();    
            System.out.print("The Loan Principal is ");
            System.out.print(". The loan's interest rate is ");
            System.out.print(dblInterestRate[box]) ;
            System.out.print("% for a term of ");
            System.out.print(intLoanTermYear[box]) ;
            System.out.println(" years.");
            System.out.print("The monthly payment will be ");
            System.out.println(CurrencyFormat.format(dblMonthlyPayment[box] ));
             while ( dblPrincipal > .01)
                    /* Calculation formulas*/
                    dblCurrentMonthlyInterest = dblPrincipal * dblMonthlyInterestRate[box];
                    dblCurrentMonthlyPrincipalPayment = dblMonthlyPayment[box] - dblCurrentMonthlyInterest;
                    dblPrincipal = dblPrincipal - dblCurrentMonthlyPrincipalPayment;
                    dblPrincipal = dblLoanBalance;

                    System.out.println (intlinecounter +"\t"+ CurrencyFormat.format(dblMonthlyPayment)+ "\t\t" + CurrencyFormat.format(dblLoanBalance)+ "\t\t"+ CurrencyFormat.format (dblCurrentMonthlyInterest));

                    if (intTrackNum20Payment == 20)
                        System.out.println("Press Enter Key to view 20 more payments");
                                {  System.in.read();
                                }catch (Exception e){;
                    intTrackNum20Payment = 0;

6 Years
Discussion Span
Last Post by hmho

I don't see how your catch statement could possibly generate that error. You misaligned your code. Where in the code does the error actually occur?


System.out.println (intlinecounter +"\t"
+ CurrencyFormat.format(dblMonthlyPayment)
+ "\t\t" + CurrencyFormat.format(dblLoanBalance)
+ "\t\t"+ CurrencyFormat.format (dblCurrentMonthlyInterest));

Instead of printing a array u should specify which array to print of.
This Bug is due to you are trying to convert an array to curreny but not the single object in the Array.

replace dblMonthlyPayment with dblMonthlyPayment[box]

i think it shd be work.

please feel free to revert. :P

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.