【问题标题】:Getting an Out of Bounds exception and I don't know how to trace back to find where the exception is获取越界异常,我不知道如何回溯以查找异常在哪里
【发布时间】:2014-04-17 00:42:09
【问题描述】:

首先,我希望这个社区能够帮助那些刚刚学习编程的人,我希望你能容忍我。 这是我正在使用的文件: DNA sequencing program

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 14


 at java.lang.String.charAt(String.java:658)
    at DNA.gap_score(run_dna.java:64)
    at DNA.dna_ini(run_dna.java:37)
    at DNA.<init>(run_dna.java:17)
    at run_dna.main(run_dna.java:243)

我收集到的是,在我的代码中的某个地方,我试图引用字符串长度之外的东西。我想知道如何使用错误代码回溯程序以找到我出错的地方。因为这里要求的是这里的完整代码:

class DNA //implements Score_card
    {
     private int row = 10;// initialize number of rows for the matrix
     private int column = 10;//initialize number of columns for the matrix
     private int opt_number = 0;//the placeholder for the number of matches in the optimal alignment

     private String DNA1 = DNA_gen(row);// initialize a String that is the length() of row
     private String DNA2 = DNA_gen(column);//initialize a String of length() column

     private int[][] dnamat = dna_ini();//initialize the DNA matrix
     private char[][] mat_hold = new char[row][column];




     private String DNA_seq1 = null;//create and empty String to be used in the matrix building/sequencing program
     private String DNA_seq2 = null;// ditto
     private String DNA_align = null;//create a String to hold the vertical alignment lines



     public int[][] dna_ini() 
        {
         //initializes the array, taking into account the possibility of and advanced gap scoring algorithm
         String a = DNA1;
         String b = DNA2;
         int[][] ini = new int[a.length()+1][b.length()+1];//initialize the size of the array to be the length()s of the String plus one
         for(int j = 0;j < (a.length()+1); j++) /*for loop to run through value of j limited by size of the array*/
            { 
             ini[j][0] = gap_score(j,0,a,b);//utilize the gap score to initialize the first row
            }//end for loop
         for(int i = 0; i < (b.length() + 1);i++)//same as above
            {
             ini[0][i]= gap_score(0,i,a,b);//same as above
            }//end for loop
         return ini;
        }//end dna ini

     public int score(int a, int b, String i, String j)
        {
         String dna1 = i;//Strings passed
         String dna2 = j;
         int a1 = a;//integers passed
         int b1 = b;
         int score = 0;//holds score
         if(i.charAt(a1) == j.charAt(b1))//if the two values in the two Strings are equivalent to one another
            {
             score = 1;// score is equal to one
            }
         return score;//return the score
        }// end score

    public int gap_score(int a, int b,String k, String l)
        { 

         int penscore = 0;
         if(k.charAt(a) != l.charAt(b))
            {
             return penscore;
            }
         else
            return penscore;
        }
     public int mismatch(int a, int b,String i,String j)
        {
         String dna1 = i;//Strings passed
         String dna2 = j;
         int a1 = a;//integers passed
         int b1 = b;
         int score = 0;//holds score
         if(i.charAt(a1) != dna2.charAt(b1))//if the two values in the two Strings are equivalent to one another
            {
             score = 0;// score is equal to one
            }
         return score;//return the score
        }// end score

    public static String DNA_gen(int n)// takes an int to determine length() of String returned
        {
         char[] S = new char[n];         //and array of character S  equals a new array of characters of size n ( parameter passed to the method
         String DNAgenchar = "AGCT";//String holds all of the possible nucleotides found in a DNA sequence
         for(int i = 1; i < n; i++)// for new variable i such that i equals 0, i is less than the length() desired, i is incremented
            { 
             int r = (int)(Math.random()*4);// given variable r is equal to a random number between 0-3
             S[i] = DNAgenchar.charAt(r);// for a given index ( as the array is cycled through) the random number generated
             //is the index of the String from which a character is pulled and added to the array
            }
         S[0] = ' ';// add a space to the beginning of the array
         String DNAgened = null;// initialize a String to be returned
         DNAgened += S; // convert S into a String for return        
         return DNAgened;//return the String 
        }

    public void  Matrix_fill()
        {
            String dna1 = DNA1;//initialize a variable to point to the dna String
            String dna2 = DNA2;


            int maxint = 0;// set the maxint integer to 0 for initialization
            int [][] matrix = dnamat;//initialize a matrix that points to the main one
            for(int j = 1; j < dna1.length()+1; j++)
                {
                 for(int i = 1; i < dna2.length()+1; i++)//cycle through the matrix
                    {
                     int diag = dnamat[j-1][i-1] + score((i-1),(j-1),dna1,dna2) + mismatch((i-1),(j-1),dna1,dna2);//diagonal value
                     int left = dnamat[j-1][i] + gap_score((j-1),i,dna1,dna2);//left value
                     int top = dnamat[j][i-1] + gap_score(j,(i-1), dna1, dna2);//top value
                     maxint = Math.max(Math.max(diag,left),top);//gets the maxintimum value of the three numbers


                    if(maxint == diag)//series of if statements to add to the String containing the directions taken by the matrix in the program
                        {
                         mat_hold[j][i] = 'd';
                         dnamat[j][i] = maxint;// if the number is taken from the diagonal
                        }
                    else if(maxint == left)
                        {
                         mat_hold[j][i] = 'l';
                         dnamat[j][i] = maxint;
                        }
                    else if(maxint == top)
                        {
                         mat_hold[j][i] = 't';
                         dnamat[j][i] = maxint;
                        }
                    }//end for loop
                }//end outer for loop
            opt_number = matrix[dna1.length() + 1][dna2.length() + 1];// set the optimum number variable to the last place in the array, containing the number of matches in the optimum configuration
        }//ends matrix fill

    public void backtrack()
        {
         char d = 'd';//initialize a series of characters to represent looked for directions
         char l = 'l';
         char t = 't';
         char temp = ' ';//not sure why i put this here

         String j = DNA1;// create two Strings to be used for indexing purposes 
         String k = DNA2;

         int e = k.length();//goes to the last row
         int x = j.length();//goes to the last column
         char[][] mat_hold2 = mat_hold;
         char opt_path = mat_hold[x][e];//initialize the beginning of the path to be last element in the array

         while(opt_path != mat_hold2[0][0])//while the current index does not equal the first element in the array
            {
             if(opt_path == d)// if the element in d was obtained from the diagonal
                {
                 DNA_seq1 = j.charAt(x) + DNA_seq1;//using the matrix location [x][e]take the element from the DNA sequence and insert it into the front of the array we are building
                 DNA_seq2 =  k.charAt(e) + DNA_seq2;//ditto
                 DNA_align = "|" + DNA_align;
                 mat_hold[x][e] = mat_hold[x-1][e-1];//since they are the same in this case, insert a line to connect them
                 opt_path = mat_hold[x][e];//set the opt path to the diagonal value to be searched
                }//since in this case it was taken from the diagonal, that means both letters were the same and so both letters at the given
                //indexes are inserted into the newly constructed sequence
             else if(opt_path == l)
                {
                 DNA_seq1 = "_" + DNA_seq1;
                 DNA_seq2 = k.charAt(e) + DNA_seq2;
                 DNA_align = " " + DNA_align;
                 mat_hold[x][e] = mat_hold[x][e-1];
                 opt_path = mat_hold[x][e];
                }
             else if(opt_path == t)
                {
                 DNA_seq1 = j.charAt(x) + DNA_seq1;
                 DNA_seq2= "_" + DNA_seq2;
                 DNA_align = " " + DNA_align;
                 mat_hold[x][e] = mat_hold[x-1][e];
                 opt_path = mat_hold[x][e];
                }
            }//end while
            System.out.println(DNA_seq1);
            System.out.println(DNA_align);
            System.out.println(DNA_seq2);
        }//end backtrack

    public void run_DNA_seqs()
        {
         Matrix_fill();
         backtrack();
        }

}//end class DNA

class adv_score extends DNA
    { 
     public int score(int a, int b, String i, String j)
        {
         String dna1 = i;//Strings passed
         String dna2 = j;
         int a1 = a;//integers passed
         int b1 = b;
         int score = 0;//holds score
         if(i.charAt(a1) == j.charAt(b1))//if the two values in the two Strings are equivalent to one another
            {
             score = 2;// score is equal to one
            }
         return score;//return the score
        }// end score

     public int mismatch(int a, int b,String i,String j)
        {
         String dna1 = i;//Strings passed
         String dna2 = j;
         int a1 = a;//integers passed
         int b1 = b;
         int score = 0;//holds score
         if(i.charAt(a1) != j.charAt(b1))//if the two values in the two Strings are equivalent to one another
            {
             score = -1;// score is equal to one
            }
         return score;//return the score
        }// end score
    }

class adv_gap extends DNA
    {
     public int gap_score(int a, int b,String k, String l)
        { 

         int penscore = -2;
         if(k.charAt(a) != l.charAt(b))
            return penscore;
         else
            penscore = 0;
            return penscore;
        }
    }
public class run_dna
    {
     public static void main(String[] args)
         {
          DNA dna1 = new DNA();
          dna1.run_DNA_seqs();
         }
    }

'

哇哦,现在出现不同的错误!

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at DNA.Matrix_fill(run_dna.java:136)
    at DNA.run_DNA_seqs(run_dna.java:194)
    at run_dna.main(run_dna.java:249)

我查看了代码,由于某种原因,当它将一个字符分配给 char[][] 数组 mat_hold 中的一个点时,它抛出了另一个异常错误,这次是 10(恰好是大小数组 10x10)

【问题讨论】:

  • 我将从DNA 包中run_dna.java 文件第64 行的gap_score 类开始...DNA.gap_score(run_dna.java:64)
  • 在此处包含代码。

标签: java string dna-sequence


【解决方案1】:

dna_ini看这段代码:

for(int j = 0;j < (a.length()+1); j++) 
{ 
    ini[j][0] = gap_score(j,0,a,b);
...

这是来自gap_score

public int gap_score(int a, int b,String k, String l)
{ 
    int penscore = 0;
    if(k.charAt(a) != l.charAt(b)) 
...

您正在使用 j0 循环到 a.length()+1。那应该是a.length()

你现在拥有它的方式,比如说a = "hi"a.length()2,所以你从 0 循环到 3j01 时的前 2 次迭代会很好。但是在下一次迭代中,当j2 时,在gap_score() 中你会使用k.charAt(2)k 现在是a,可怕的变量名)。由于 k (nee a) 的长度只有 2,所以这会爆炸。

请,请,请避免无意义的变量名称。您可能会节省一些击键,但您会花费更多时间来尝试弄清楚发生了什么。

编辑:我想我知道你现在想要做什么。

for(int j = 1;j < (a.length()+1); j++) 
{ 
    ini[j][1] = gap_score(j-1,0,a,b);
}
for(int i = 1; i < (b.length() + 1);i++)
{
    ini[1][i]= gap_score(0,i-1,a,b);
...

这最终会得到ini[][] 的样子:

0 0 0 0 0 0 0 0
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?
0 ? ? ? ? ? ? ?

其中?s 是来自gap_score() 的值。 尝试一下,看看你是否得到了你想要的结果。

【讨论】:

  • 但我认为我必须放 length + 1 因为在 2D 数组的初始化中,为了在矩阵中创建缓冲区空间,必须创建额外的行和列,所以它看起来像这样avatar.se/molbioinfo2001/dynprog/initial.gif
  • 这对于初始化 2d 数组很好,但它不会改变您尝试访问的字符比 String 包含的字符多的事实。
  • 哇,我不可能;相信我错过了。非常感谢你,这几天我脑子里一直在琢磨代码,我写的时候并没有弹出错误。
  • 好吧,现在我遇到了这个错误,与之前的类似,但我认为这是一个不同的问题Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at DNA.Matrix_fill(run_dna.java:136) at DNA.run_DNA_seqs(run_dna.java:194) at run_dna.main(run_dna.java:249)
  • 类似,唯一的区别是您尝试访问的数组索引大于 array.size-1 而不是String 索引。查看它告诉你的行 (136) 并找出它发生的原因。
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多