简体   繁体   English

索引超出长度范围。 找不到错误

[英]Index out of bounds for length. Cant find error

I searched through other posts for someone having the same error which was similar to my code, but i could not find it.我在其他帖子中搜索了与我的代码相似的相同错误的人,但我找不到它。 the current issue i am having is with my variables totalRate[] and years[].我目前遇到的问题是我的变量 totalRate[] 和 years[]。 I can not input a number greater than 6 into either arrayList, while my clientNames[] and amountInvested[] are working just fine.我不能在 arrayList 中输入大于 6 的数字,而我的 clientNames[] 和 amountInvested[] 工作得很好。 Can anyone spot my error?谁能发现我的错误?


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

Im 99% positive it is in the last for loop.我 99% 肯定它在最后一个 for 循环中。 My best guess at the moment is that it is something with this line...我目前最好的猜测是这条线是什么......

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

Edit: I completley missed this in my review, here is the error code i am getting.编辑:我在评论中完全错过了这个,这是我得到的错误代码。

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)

The problem indeed lies in search = table [years[j]] [totalRate[j]];问题确实在于search = table [years[j]] [totalRate[j]];

What are their values?他们的价值观是什么? If I enter 20 years into the years table, then the program tries to get the value from table[20] .如果我在年表中输入 20 years ,那么程序会尝试从table[20]中获取值。 As you can see, years[j] and totalRate[j] are not indexes.如您所见, years[j]totalRate[j]不是索引。

A good way to find out these problems is to use a debugger or to print different variables to console often.找出这些问题的一个好方法是使用调试器或经常将不同的变量打印到控制台。

I'm not actually quite sure what you want to do with table , so sadly I can't really help with that我实际上不太确定你想用table做什么,所以很遗憾我对此无能为力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串索引超出范围错误,无法找到错误的根源 - String index out of bounds error, cant find the root of the error 索引 4 超出 4 长度的范围 - index 4 is out of bounds for 4 length 索引 (-1, 5) 超出长度 5 的范围? - Index (-1, 5) out of bounds for length 5? 计算数组元素的出现次数 - 索引超出范围错误但索引号和超出范围长度是相同的数字? - Counting occurrences of an array element - Index out of bounds error but the index number and out of bounds length are the same number? ArrayIndexOutOfBoundsException:索引 3 超出长度 3 的范围 - ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 如何在Java中纠正Arraylist索引超出长度错误的范围 - How to correct Arraylist index out of bounds for length error in Java Arraylist java.lang.IndexOutOfBoundsException:索引 3 超出长度 3 错误的范围 - Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error 如何修复错误ArrayIndexOutOfBoundsException:索引1超出长度1的范围? - How to fix thid error ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1? 添加到二维数组返回“索引 0 超出长度 0 的范围”错误 - Adding to 2D Array returning 'Index 0 out of bounds for length 0' error 字符串生成器初始化长度,索引越界错误 - String builder initialized length,index out of bounds error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM