【问题标题】:Index out of bounds for length. Cant find error索引超出长度范围。找不到错误
【发布时间】:2020-10-24 16:17:27
【问题描述】:

我在其他帖子中搜索了与我的代码相似的相同错误的人,但我找不到它。我目前遇到的问题是我的变量 totalRate[] 和 years[]。我无法在任一 arrayList 中输入大于 6 的数字,而我的 clientNames[] 和 amountInvested[] 工作正常。谁能发现我的错误?


public class Lab04_Part_1 {
  @SuppressWarnings("null")
public static void main(String[] args) {

      Scanner s = new Scanner(System.in);
      double search = 0;
      double [] [] table = {  {1.05, 1.06, 1.07, 1.08, 1.09, 1.10},//row 1
                {1.1025, 1.1236, 1.1449, 1.1664, 1.1881, 1.21}, //row 2
                {1.1576250, 1.1910160, 1.2250430, 1.2597120, 1.2950290, 1.331}, //row 3
                {1.2155063, 1.2624770, 1.3107960, 1.3604890, 1.4115816, 1.4641},//row 4
                {1.2762816, 1.3382256, 1.4025517, 1.4693281, 1.5386240, 1.61051}, //row 5
                {1.3400956, 1.4185191, 1.5007304, 1.5868743, 1.6771001, 1.771561}, //row 6
                {1.4071004, 1.5036303, 1.6057815, 1.7138243, 1.8280391, 1.9487171}, //row 7
                {1.4774554, 1.5938481, 1.7181862, 1.8509302, 1.9925626, 2.1435888}, //row 8
                {1.5513282, 1.6894790, 1.8384592, 1.9990046, 2.1718933, 2.3579477}, //row 9
                {1.6288946, 1.7908477, 1.9671514, 2.1589250, 2.3673637, 2.5937425} //row 10
    }; // end declaration and initialization of 2D array

    // Declare String [ ] and int array(s) here
      final int totalClients = 5;
      String [] clientNames = new String[totalClients];
      int [] totalRate = new int[totalClients];
      int [] years = new int[totalClients];
      int [] amountInvested = new int[totalClients];
    
    // Declare for loop used for input here
      for(int i = 0; i < totalClients; i++) 
      {
          System.out.println("Enter Name of client number " + (i+1));
          clientNames[i] = s.nextLine();
          
          System.out.println("How much would you like to invest? (100-10000)");
          amountInvested[i] = Integer.parseInt(s.nextLine());
          
          System.out.println("What is the interest rate?");
          totalRate[i] = Integer.parseInt(s.nextLine());
          
          System.out.println("How long will your investment gain interest? ");
          years[i] = Integer.parseInt(s.nextLine());
          
          
      }
    
    // Declare for loop for output here
      for(int j = 0; j < totalClients; j++) 
      {
          System.out.print(clientNames[j]);
          
          search = table [years[j]] [totalRate[j]];
          double finalBalance = amountInvested[j] * search;
          System.out.printf("     Compound value: $%.2f", finalBalance);
          
          double amountGained = finalBalance - amountInvested[j];
          System.out.printf("       Interest: $%.2f", amountGained);
          
          System.out.println("");
      }
      


 } // end main
} // end class

我 99% 肯定它在最后一个 for 循环中。目前我最好的猜测是这条线的东西......

search = table [years[j]] [totalRate[j]];

编辑:我在评论中完全错过了这个,这是我得到的错误代码。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 6
    at Lab04_Part_1.main(Lab04_Part_1.java:51)

【问题讨论】:

  • 尝试打印出years[j]totalRate[j] 如果其中一个>= 到任何一个表格尺寸,您就有问题。
  • 请发布异常的堆栈跟踪(前几行)

标签: java arrays indexing


【解决方案1】:

问题确实出在search = table [years[j]] [totalRate[j]];

他们的价值观是什么?如果我在years 表中输入20 年,那么程序会尝试从table[20] 中获取值。如您所见,years[j]totalRate[j] 不是索引。

找出这些问题的一个好方法是使用调试器或经常将不同的变量打印到控制台。

我不太确定你想用 table 做什么,所以很遗憾我帮不上忙

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多