【问题标题】:I Keep Getting this Error: java.lang.ArrayIndexOutOfBoundsException: 12 [closed]我不断收到此错误:java.lang.ArrayIndexOutOfBoundsException:12 [关闭]
【发布时间】:2013-03-19 21:50:03
【问题描述】:

我的程序基本上采用 ISBN 编号列表并确定它们是否有效。所以错误在: s1[l]=digits[i][l-1]+digits[i][l];这行代码使用 for 循环获取 ISBN 编号的部分总和。它下面的另一个循环取数组 s1 中所有整数的部分和。请帮忙?错误是:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at ISBN.main(ISBN.java:67)

  import java.util.*;
  import java.io.*;

  public class ISBN {
  public static void main(String [] args) throws IOException{
//Reads file line by line
  File ISBNFile = new File ( "isbn_input.txt" );
  Scanner input = new Scanner(ISBNFile);
  String [] preISBN = new String [9999];
  int i = 0;
  int j =0;
  int l =0;
  int m = 0;
  int count =0;
  while (input.hasNextLine()){
    String line = input.nextLine();
    preISBN [i] = line;
    i++;
    count++;
                             }
    input.close();

    //Makes new array with specified count
    String [] ISBN = new String [count];
    String [] ISBNresult = new String [count];
 for(i=0;i<ISBN.length;i++){
   ISBN [i] = preISBN [i];
                           }
// int [] s1 = new int =
 int [] [] digits = new int [ISBN.length] [12];
 int [] s1 = new int [12];
 int [] s2 = new int [12];
 //Loads digits [] [] with values that will later be summed
 for(i=0;i<ISBN.length;i++){
   if(ISBN[i].length()!=12){
     ISBNresult [i] = "Invalid";
     continue;       
   }

   for(j=0;j<12;j++){

      if(ISBN[i].charAt(j)=='X'||ISBN[i].charAt(j)=='x'){
        digits [i] [j] = 10;
      }
      else if(ISBN[i].charAt(j)=='-'){
        digits [i] [j] = 0;
      } 
      else   
      if(ISBN[i].charAt(j)==1||ISBN[i].charAt(j)==2||ISBN[i].charAt(j)==3||ISBN[i].charAt(j)==4||ISBN[i].charAt(j)==5||ISBN[i].charAt(j)==6||ISBN[i].charAt(j)==7||ISBN[i].charAt(j)==8||ISBN[i].charAt(j)==9){                                                     
      digits [i][j] = Character.getNumericValue(ISBN[i].charAt(j)); 
      }
      else{
      ISBNresult[i] = "Invalid";
      break;
      }
                    }


 }
 for(i=0;i<ISBN.length;i++){
   for(j=0;j<ISBN[i].length();j++){
     if(ISBN[i].length()!=12){
       ISBNresult [i] = "Invalid";
       continue;         
     }
     s1[0]=digits[i][0];
     for(l=1;l<=12;l++){
     s1[l]=digits[i][l-1]+digits[i][l];
     }

     s2[0]=s1[0];
     for(m=1;m<=12;m++){
     s2[m]=s2[m-1]+s1[m];    
     }
   }
   if(s2[12]%11==0){
     ISBNresult[i]="Valid";   
   }
   else{
     ISBNresult[i]="Invalid";
   }
 }

      File outFile = new File("isbn_output.txt");
      FileWriter fWriter = new FileWriter (outFile, true);
      PrintWriter pWriter = new PrintWriter (fWriter);
      for(i = 0;i<ISBN.length;i++){
  pWriter.println (ISBN[i]+ " "+ ISBNresult[i] );

  }
  pWriter.close();
       }
      }

【问题讨论】:

  • 你应该重构你的代码。尝试创建负责解析文件的类并添加一些小方法。还可以尝试使用列表/集合而不是 Sayem Ahmed 所写的数组。

标签: java arrays eclipse int indexoutofboundsexception


【解决方案1】:

这一切正在发生,因为您正在为数组s1s212th 索引赋值。当ISBN[i].length &gt;= 12 时,你也没有打破循环。 你的 for 循环应该是这样的:

 for(i=0;i<ISBN.length;i++){
   for(j=0;j<ISBN[i].length();j++){
     if(ISBN[i].length()<=12){
       ISBNresult [i] = "Invalid";
       continue;         
     }
     else if (ISBN[i].length() > 12)
     {
         break;
     }

     s1[0]=digits[i][0];
     for(l=1;l<=12;l++){
     s1[l]=digits[i][l-1]+digits[i][l];
     }

     s2[0]=s1[0];
     for(m=1;m<12;m++){
     s2[m]=s2[m-1]+s1[m];    
     }
   }
   if(s2[11]%11==0){
     ISBNresult[i]="Valid";   
   }
   else{
     ISBNresult[i]="Invalid";
   }
 }

【讨论】:

    【解决方案2】:

    数组定义为12。

    int [] [] digits = new int [ISBN.length] [12];
    
    s1[0]=digits[i][0];
    for(l=1;l<=12;l++){
        s1[l]=digits[i][l-1]+digits[i][l];
    }
    

    您在 [l] 处超出,其中 l == 12。

    [l] 从 0 开始直到 11。

    【讨论】:

      【解决方案3】:
      for(l=1;l<=12;l++){
          s1[l]=digits[i][l-1]+digits[i][l];
      }
      

      l 在最后一次迭代中变为12,有效地使加法语句的第二部分像这样。

      digits[i][12]
      

      s1[l] 也是如此,变成s1[12],因为两者都是这样声明的

      int [] [] digits = new int [ISBN.length] [12];
      int [] s1 = new int [12];
      

      永远记住数组的最大可访问索引是array.length-1。因此,对于长度为 12 的数组,最后一个索引将是 11

      【讨论】:

        【解决方案4】:

        您已将 s1 声明为

         int [] s1 = new int [12];
        

        它将有 12 个元素,但索引是从 0 到 11。

        在下面的代码中

           for(l=1;l<=12;l++){
             s1[l]=digits[i][l-1]+digits[i][l];
             }
        

        当 l = 12 时,您将结果等同于 s[12]。

        但是没有s[12],(最大值是s[11])所以报错。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-15
          • 2012-05-17
          • 1970-01-01
          • 1970-01-01
          • 2021-11-06
          • 2020-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多