2013-08-05 122 views
2

有人可以帮我解决以下错误吗?该程序运行,但我得到一个关于我的主要显示捐款被称为错误。我如何解决这个问题,让它运行?它是否与语法错误有关?这是我第一次使用数组,所以我欢迎有良好习惯的反馈。谢谢!Java数组索引超出范围?

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 
+0

(i + 1)= 6当我= 5,你得到的错误,因为最后一个索引是5 – VirtualTroll

+0

所以我必须做到零到5? – user2644085

+0

我想,为了第一次任务的目的,你可能不得不使用数组,但只有当你能够尽快发现集合时,它提供了很好的动态可扩展数据结构(列表,集合等)。 – andi

回答

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

这是一个有6个空格的数组,正确。但是:

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

在这里,您访问的是7个空格。 0,1,2,3,4,5和6

+0

是否存在我可以放入的代码,以便从一开始就启动? – user2644085

+0

没有。数组从0开始。你*当然可以只留下未使用的第一个空格(0)。 –

+0

好的,谢谢大家。我会标记为正确的!我喜欢这个网站:) – user2644085

3

你在你的代码的几个地方,你在你的代码参照指数6直接。这里有几个:

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

但所有的阵列被宣布为长度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,所以长度n的阵列仅具有索引0通过n - 1,或通过0这里5。如果您需要索引6,请将您的数组长度设置为7

0

我注意到您已经用一种普通的 “关断接一个” 错误¯\ _(ツ)_ /¯

在这里,你“VE正确声明3个阵列的6大小:

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

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

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

虽然有,实际上,每个阵列中6个元素的第一个元素被称为0

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

在您的代码中调用第7个元素“ [6]”不存在:

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]; 

要修复它,你只需要让你的元素从0到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]; 

这是一个容易犯的错误,因为我们已经告诉我们, 0我们的整个生命都没有任何价值。

继续编程,不要放弃!