0

首先,我想比这个社区更慷慨地帮助那些刚刚学习编程的人,我请求你多多包涵。这是我正在使用的文件: DNA测序程序

    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)

4

1 回答 1

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)) 
...

你正在循环jfrom 0to a.length()+1。应该是这样a.length()的。

假设你现在拥有它的方式a = "hi"a.length()2这样你从0到循环3。前 2 次迭代何时j0并且1将是好的。但是在下一次迭代时j2gap_score()会这样做k.charAt(2)(现在ka可怕的变量名)。由于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()。试试看,看看你是否得到你想要的结果。

于 2014-04-17T01:11:36.143 回答