I came across this post " pass array to method Java " on how to pass an array to a method however when trying to adapt it to my intrest calculator Ive got going I get errors such as "the operator / is undefined for the argument types double[],double" and I am unable to get it resolved. I am also looking to return the whole array since I need to print the results later on and eventually sort them.
For example I define my arrays and how big they can be and then call on the user to input loan amount, interest, and frequency calculated. After I get the data and its all in array format I then pass the whole arrays off to calculate simple interest and want to return the results as an array, and then pass the same initial arrays to find compound interest by month, week, and day but I get the previously mentioned error when it tries to do the division involved in "A = P(1+ (r/n))^(n*t)" The gathering data works fine I just cannot seem to pass arrays correctly or loop through them once I do IF I'm passing them right
Any and all assistance is appreciated as always.
Relevent code
call data from user
do {
arrPrincipalAmt[i] = getPrincipalAmount();
arrInterestRate[i]= getInterestRate();
arrTerm[i] = getTerm();
i++;
if (i < 4)
{
/*
* if "i" comes to equal 4 it is the last number that will fit in the array of 5
* and will Skip asking the user if they want to input more
*/
System.out.println("Enter another set of Data? (Yes/No):");
boolean YN = askYesNo();
if (YN == true)
{
more = true;
}
else if (YN == false)
{
more=false;
}
}
else
{
more = false;
}
}while(more);
while (run)
{
//Start calculations call methods
final int dINy = 365; //days in year
final int mINy = 12; //months in year
final int wINy = 52; //weeks in year
double loanYears =(double) arrTerm[i]/mINy; //convert loan months into fraction of years, or whole years with decimals.
arrSimple= calculateSimpleInterest(arrPrincipalAmt, arrInterestRate,loanYears);
//Simple IntrestloanAmount * (annualInterestRate/100.0) * loanYears;
arrCompoundMonthly = calculateCompundInterest(arrPrincipalAmt, arrInterestRate,mINy,loanYears);
//rewrite month compound:loanAmount * Math.pow((1+((annualInterestRate/100)/mINy)),(loanYears*mINy)) - loanAmount;
simple interest that fails
public static double[] calculateSimpleInterest(double[] arrPrincipalAmt, double[] arrInterestRate, double Years)
{
for(int i=0; i<arrPrincipalAmt.length; i++)
{
double simpInterest= arrPrincipalAmt[i] * (arrInterestRate[i]/100.0) * Years; //Simple Interest Calculation
}
return simpInterest;
}
compound interest that fails
public static double[] calculateCompundInterest(double[] arrPrincipalAmt, double[]
arrInterestRate, double frequency, double time)
{
/*The Compound Interest calculation formation is as follows:A = P(1+ (r/n))^(n*t) - P
A = total interest amount only.
P = principal amount (loan amount or initial investment).
r = annual nominal interest rate (as a decimal not a percentage).
n = number of times the interest is compounded per year.
t = number of years (don't forget to convert number of months entered by user to years).
*/
for (int i=0; i < arrPrincipalAmt.length; i++)
{
double[] compound= arrPrincipalAmt[i] * Math.pow((1+(arrInterestRate[i]/100.0)/frequency),(frequency*time)) - arrPrincipalAmt[i]; //Compound Interest monthly, weekly and daily depending on the frequency passed
}
return compound;
}