Amortization table using array, getting precision error

getting precision error when using double variable types

Page 1 of 1

1 Replies - 4140 Views - Last Post: 24 December 2007 - 10:25 AM Rate Topic: -----

#1 ricogrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-December 07

Amortization table using array, getting precision error

Posted 24 December 2007 - 10:00 AM

I a getting this error on my code on every part where I use my term variable (mtgNp) to define the array length. Here is one example. I am also not sure how to print the array out but I have not gotten that far or there are no compile errors on it but I doubt that works either.

D:\JAVA\part 3\MtgCalcAmort.java:76: possible loss of precision
found : double
required: int
double[] loanBal = new double[mtgNp]

I have casted mtgNp (the term) as an integer right above where I get this error. I either need to figure this out or try something else and remove arrays, this is due today!

double payment = MortgageUtils.calculateMtgPmt(
							 mortgage.getMtgPv(),
							 mortgage.getMtgNp(),
							 mortgage.getMtgApr());

			double mtgPv = mortgage.getMtgPv();

			double mtgNp = mortgage.getMtgNp();
			mtgNp = (int) mtgNp;

			double mtgApr = mortgage.getMtgApr();

				// declare variables used for amortization table
							double tableAnswer = 0;
							int i;
							int[] paymentNumber; // tracks payment #

							double totalLoanBal = payment * mtgNp; // define totalLoan balance

							double totalIntPaid = totalLoanBal - mtgPv;//Total Interest = Ttl payments - principle

							double[] loanBal = new double[mtgNp]; //declare loanBal array to store the loan balance as it decrements

							loanBal[0] = totalLoanBal;			// set initial loanBal to totalLoanbal

							// declare the 4 variables for the interest and principle amortization

							double[] int_Paid = new double[mtgNp];

							double[] principle_Paid = new double[mtgNp];

							double[] int_Bal = new double[mtgNp];

							double[] principle_Bal = new double[mtgNp];

							// set some of initial values of above to zero

							int_Paid[0] = 0;
							principle_Paid[0] = 0;

							//set initial interest to totalBal - principle. Set initial principle balance to principle
							//entered by user

							int_Bal[0] = totalIntPaid;
							principle_Bal[0] = mtgPv;
							paymentNumber[0] = 1;

							// set up calculations for each variable array value from 0 to termos

							for (i=1; i>=mtgNp; i++)
							{

								paymentNumber[i] = paymentNumber[i] +1;

								loanBal[i] = loanBal[i-1] - payment;

								int_Paid[i] = mtgApr * principle_Bal[i-1];

								principle_Bal[i] = principle_Bal[i-1] - (payment - int_Paid[i]);

								principle_Paid[i] = principle_Paid[i-1] + (payment - int_Paid[i]);

								int_Bal[i] = int_Bal[i-1] - int_Paid[i];
							}

				//loop output while user enters 1

				boolean run_amortTable = false;
								while (!run_amortTable)
				{
						System.out.println();
						System.out.println();
						System.out.println("Would you like to see amortization table for this mortgage?");
		 				  System.out.print("Enter 1 for yes or 2 for no:  ");
						tableAnswer = UserInput.readInNumber();

		 				  // initialize answer variable


						//test to see if user wants to see another table

						if (tableAnswer < 1 || tableAnswer > 2)
						{
							System.out.print("You must enter 1 or 2. Please try again. ");
							tableAnswer = UserInput.readInNumber();
							System.out.println();
							run_amortTable = true;
						}
						else
						{
							if (tableAnswer == 1)
							{
								//clear the screen and set flag to continue, got this from Cheryl's code

								for (i=0; i<25; ++i) System.out.println();
								//code for clearing console screen found on
								//http://www.thescripts.com/forum/thread16485.html
								run_amortTable = false;

							System.out.println("														 Total ");
							System.out.println("Payment	  Principal	  Principal	Interest		Interest ");
							System.out.println("			  Paid		  Balance		 Paid		 Balance ");
							System.out.println();
							System.out.print("\n\t--------------------------------------------------------------\n");

							for (i=1; i<=mtgNp; i++)
								{
							System.out.println("paymentNumber[i]\t principle_Paid[i]\tprinciple_Bal[i]\tint_Paid[i]\t int_Bal[i]");
							System.out.print("\n\t--------------------------------------------------------------\n");
							System.out.println("														 Total ");
							System.out.println("Payment	  Principal	  Principal	Interest		Interest ");
							System.out.println("			  Paid		  Balance		 Paid		 Balance ");
							System.out.println();
							System.out.print("\n\t--------------------------------------------------------------\n");
							System.out.println();
							System.out.print("Total of all Payments is" + "$" + totalLoanBal + "\n");
								}
							}




there is more but I this is where I get the error. Whole code is attached.

This post has been edited by Martyr2: 24 December 2007 - 10:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Amortization table using array, getting precision error

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Amortization table using array, getting precision error

Posted 24 December 2007 - 10:25 AM

Well as you may have realized you can only use integers as subscripts for an array. However, when you use the following line mtgNp = (int) mtgNp; you are casting to integer but dumping it back to a variable declared as double so it ends up being double again. You need to declare an integer variable and then cast it to int and dump it to that integer variable.

I am not too sure what mtgNp represents, but if you need to use it as an array subscript that will be the problem. Remember that casting to an integer will immediately drop the fractional part of the double. Keep this in mind during any calculation you may use with this new integer value.

As for printing the array, that is simply a loop that will loop through the array using this new integer variable you have used to setup the array.

for (intMtgNp = 0; intMtgNp < int_Bal.length; intMtgNp++) {
	System.out.println("Balance: " + int_Bal[intMtgNp]);
}



The above code assumes that intMtgNp is the integer variable you setup and casted from double to get the subscript. We use it to loop the array based on its length and simply print the word "Balance:" and then the value.

Hopefully that makes some sense.

Enjoy!

"At DIC we be coding santas, sharing DIC all over the world!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1