-1

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;
  }
Community
  • 1
  • 1
Spartan-196
  • 363
  • 2
  • 4
  • 15
  • 2
    Small relevant code *inline* with the post or it didn't happen. In any case, that means there was code such as `doubleArray / double` which is nonsense. –  Apr 01 '12 at 21:56
  • 4
    please paste here the *relevant part* of your code – amit Apr 01 '12 at 21:56

2 Answers2

3

If you are getting an error like the operator / is undefined for the argument types double[],double it means that you are trying to divide the entire array by a value rather than a single element.

Change a line like myarray / value to myarray[i] / value

John B
  • 32,493
  • 6
  • 77
  • 98
  • I see that you did answer before I edited with the code snippets but did already attempt this solution which elevated it but ended in a type mismatch for cannot convert double to double[]. the code snipped from @integeruser has given a fix for that. – Spartan-196 Apr 01 '12 at 22:43
1

i checked your code and the problem is that at line 227 you define compound as a double array while the result of the expression at that line is simply a double. I think you want to do so:

double[] compound = new double[arrPrincipalAmt.length];

for (int i=0; i < arrPrincipalAmt.length; i++) {
    compound[i] = arrPrincipalAmt[i] * Math.pow((1+(arrInterestRate[i]/100.0)/frequency),(frequency*time)) - arrPrincipalAmt[i];
}

return compound;
  • I am new to working with arrays let alone ones inside of methods making an new array the size of the length of another did resolve the errors I was receiving and continue my debug. Much appreciated. – Spartan-196 Apr 01 '12 at 23:02