0

I have a table full of calculations. Each row contains numbers based on personal debt. For example:

Row 1: Debt 1 (Name), 7.9 (APR), 1000 (Balance), 20 (MinPayment);

Row 2: Debt 2 (Name), 9.9 (APR), 2000 (Balance), 40 (MinPayment);

etc..

There are 6 rows in this test. I am doing a few loops with a few arrays and I got the above error (in title).

05-17 18:31:47.548: E/AndroidRuntime(2019): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

Which is odd because length IS 6?

Here is the entire Method:

 public int payoffDebt(Double totalDebt) {
    Cursor c = database.rawQuery("SELECT *  FROM debt;", null);
    int monthTotal = 0; 
    double interestFee = 0;
    double interestFeeTotal = 0;

    String indBal[] = new String[c.getCount()];
    String indPay[] = new String[c.getCount()];
    String indApr[]  = new String[c.getCount()];
    double totalBal[]  = new double[c.getCount()];
    double totalInterestFees[]  = new double[c.getCount()];
    int rowCounter[] = new int[c.getCount()];

    int j = 0; int k = 0;   
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        // get individual Bal, APR percent, Payment, store into three arrays;
        indBal[k++]  = c.getString(c.getColumnIndex("debt_total"));
        indPay[k++] = c.getString(c.getColumnIndex("payment"));
        indApr[k++] = c.getString(c.getColumnIndex("apr"));
        rowCounter[k++] = k;
    }
    c.close();

    while (totalDebt >= 0) {

        for (int i : rowCounter) {
            interestFee = (((Double.valueOf(indApr[i]) / 100) / 12) * Double
                    .valueOf(indBal[i]));
            totalDebt = totalDebt
                    - (Double.valueOf(indPay[i]) - interestFee);
            interestFeeTotal += interestFee; // sum of all Apr Fees CURRENT
                                                // month in while loop
        }

        totalBal[j++] = totalDebt; // record total debt for this after all
                                    // payments
        totalInterestFees[j++] = interestFeeTotal; // record total interest
                                                    // fees for this month
                                                      // from all debt

        // Increment month
        monthTotal += 1;

    }
    return monthTotal;

The problem was line 165 :

indApr[k++] = c.getString(c.getColumnIndex("apr"));
4
  • Do you really want to increment k all that many times? Commented May 18, 2012 at 1:41
  • Well I need each array with K++ to increase by one on the next iteration... to make room for new data.. or at least I thought Commented May 18, 2012 at 1:47
  • 3
    Yet you are incrementing k four times for each loop iteration Commented May 18, 2012 at 1:50
  • ah... there lies the problem. they should have been unique, not all k.... Commented May 18, 2012 at 1:51

2 Answers 2

4

Java arrays start at 0, not 1. That means the first element of an array is accessed using array[0], the second with array[1], etc. As such, to access the last element of an array, use array[array.length - 1]. Using array[array.length] is one index out of bounds, hence the exception.

Sign up to request clarification or add additional context in comments.

Comments

1

Others have discussed how to fix your array index problem but maybe the root cause is that you have five separate arrays which have to be kept insync. If you made a new class to hold the five items of data:

public class Data{
    String indBal;
    String indPay;
    String indApr;
    double totalBal;
    double totalInterestFees;
}

And have a single array of Data then your loop indices should be easier to keep track of.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.