2

Can someone help me with an error posted below? The program runs, but I'm getting an error regarding my main when display donations is called. How can i fix this so it runs? Does it have to do with a syntax error? It's my first time working with arrays so I welcome feedback for good habits. Thanks!

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
        at donations.processDonations(donations.java:78)
        at donations.main(donations.java:33)

    import java.util.Scanner; //needed for input

    public class donations {
        static double[] cashDonations = new double[6]; // stores the cash donations
                                                        // from 6 sites
        static double[] lbsFood = new double[6]; // stores the pounds of food
                                                    // donated from 6 sites
        static String[] siteName = new String[6]; // stores the names of the 6 sites
        static String bestSiteCash = " "; // stores the site name with the highest
                                            // cash donation
        static String bestSiteFood = " "; // stores the site name with the highest
                                            // food donation
        static double totalCash = 0; // stores the total cash collected for all
                                        // sites
        static double totalFood = 0; // stores the total food pounds collected for
                                        // all sites
        static double maxFood = 0; // store the highest food collection
        static double maxCash = 0; // stores the highest cash collection

        public static void main(String[] args) {

            Scanner input = new Scanner(System.in); // needed for input
            String anotherCourse = "yes"; // variable to control running program
                                            // again
            do {

                getDonations();
                processDonations();
                displayDonations();

                // This code ends the do while to run again
                System.out.print("Enter yes if you want to run again: ");
                anotherCourse = input.next();
                input.nextLine(); // causes skipping issue to fix
                System.out.print("\n\n\n");
            } while (anotherCourse.equalsIgnoreCase("yes"));

        } // end of main

        public static void getDonations() {
            Scanner input = new Scanner(System.in); // needed for input
            // Prompt user for site info
            for (int i = 0; i < 6; i++) {
                System.out.println("Enter site " + (i + 1) + " name: ");
                siteName[i] = input.next();

                System.out.println("Enter cash donation(USD) for " + siteName[i]
                        + ": ");
                cashDonations[i] = input.nextDouble();
                // double [] cashDonations = new double [6]; You have this already
                // at the top
                // int i;
                // for (i = 0 ; i < 6 ; i++)

                // cashDonations[i] = input.nextDouble();

                // double [] lbsFood = new double [6];
                System.out.println("Enter food donation(lbs.) for " + siteName[i]
                        + ": ");
                lbsFood[i] = input.nextDouble();

            }

        }

        public static void processDonations() {
            totalCash = 0;
            totalFood = 0;
            maxCash = cashDonations[0];
            maxFood = lbsFood[0];

            totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3]
                    + cashDonations[4] + cashDonations[5] + cashDonations[6];
            totalFood = lbsFood[1] + lbsFood[1] + lbsFood[2] + lbsFood[3]
                    + lbsFood[4] + lbsFood[5] + lbsFood[6];

        }

        public static void displayDonations() {
            System.out.print("Total Cash Donations are " + totalCash);
            System.out.print("Total Food Donations are " + totalFood);
            System.out.print("Donation totals for " + siteName[1]);
            System.out.print("Total cash: " + cashDonations[1]);
            System.out.print("Food donations " +lbsFood[1]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[2]);
            System.out.print("Total cash: " + cashDonations[2]);
            System.out.print("Food donations " +lbsFood[2]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[3]);
            System.out.print("Total cash: " + cashDonations[3]);
            System.out.print("Food donations " +lbsFood[3]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[4]);
            System.out.print("Total cash: " + cashDonations[4]);
            System.out.print("Food donations " +lbsFood[4]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[5]);
            System.out.print("Total cash: " + cashDonations[5]);
            System.out.print("Food donations " +lbsFood[5]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[6]);
            System.out.print("Total cash: " + cashDonations[6]);
            System.out.print("Food donations " +lbsFood[6]);
            System.out.println();

        }// end of displayDonations()

    }// end of class
3
  • (i + 1) = 6 when i = 5 and you get the error because the last index is 5 Commented Aug 5, 2013 at 23:28
  • so I have to do zero thru 5? Commented Aug 5, 2013 at 23:30
  • I guess that for purpose of first assignments, etc. you probbably have to use arrays, but only if you can discover ASAP the collections, which offers nice dynamically expandable data structures (Lists, Sets, etc.). Commented Aug 5, 2013 at 23:31

3 Answers 3

5
static double[] cashDonations = new double[6];

That is an array with 6 spaces, correct. However:

maxCash = cashDonations[0];
totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3] + cashDonations[4] + cashDonations[5] + cashDonations[6];

Here, you are accessing 7 spaces. 0, 1, 2, 3, 4, 5 and 6.

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

3 Comments

Nope. Arrays start at 0. Always. You could of course just leave the first space (0) unused.
ok thank you everyone. I will mark as correct! I love this site :)
@user2644085 some languages do allow you to explicitly start all arrays at 1 (Visual Basic comes to mind) but it is considered very poor practice.
3

You have several places in your code where you are referring to index 6 directly in your code. Here are a few:

System.out.print("Donation totals for " + siteName[6]);
System.out.print("Total cash: " + cashDonations[6]);
System.out.print("Food donations " +lbsFood[6]);

But all of your arrays are declared to be of length 6.

static double[] cashDonations = new double[6]; // stores the cash donations
                                                    // from 6 sites
static double[] lbsFood = new double[6]; // stores the pounds of food
                                                // donated from 6 sites
static String[] siteName = new String[6];

Arrays in Java are 0-based, so an array of length n only has indices 0 through n - 1, or 0 through 5 here. If you need an index 6, make your array length 7.

Comments

0

I noticed that you've made a common "off-by-one" error ¯\_(ツ)_/¯

Here you've properly declared 3 arrays the size of 6:

    static double[] cashDonations = new double[6]; 

    static double[] lbsFood = new double[6]; 

    static String[] siteName = new String[6];

Although there are, indeed, 6 elements in each array the first element is referred to as 0

[0], [1], [2], [3], [4], [5]

In your code you call on a 7th element "[6]" that doesn't exist:

   totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3]
                + cashDonations[4] + cashDonations[5] + cashDonations[6];

   totalFood = lbsFood[1] + lbsFood[1] + lbsFood[2] + lbsFood[3]
                + lbsFood[4] + lbsFood[5] + lbsFood[6];

To fix it you just need to make your elements from 0 to 5:

   totalCash = cashDonations[0] + cashDonations[1] + cashDonations[2]
                + cashDonations[3] + cashDonations[4] + cashDonations[5];

   totalFood = lbsFood[0] + lbsFood[1] + lbsFood[2]
                + lbsFood[3] + lbsFood[4] + lbsFood[5];

It's an easy mistake because we've been taught that 0 has a value of nothing our whole lives.

Keep programming and don't give up!

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.