Sonal Akhal
Sonal Akhal

Reputation: 15

display my output in a matrix form

my application-sequence alignment

Above is the image of my application.i am build an application to do the alignment of two string.the two string will be compared and a 2d array will be populated.the values of the 2d array will be the values of the matrix.All works perfectly fine the only prob i ma having is that i want when it print the values of the matrix it prints it in a matrix format as it the example below instead of having an output of "0000000011011000002100013" i want it to display as this

   0 0 0 0 0
   0 0 1 0 0
   0 0 1 0 0
   0 1 0 2 1
   0 1 0 1 3

When print out it has to see if the number of character printed is equal to my row size and then move to next line and continues to print until all matrix values has been displayed.Below are my pieces of my codes.thank you in advance

Code for my Compute matrix button

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)        {                                         
   match_reward = Integer.parseInt (match_tf.getText());
   mismatch_penalty =Integer.parseInt (mismatch_tf.getText());
   gap_cost=Integer.parseInt(gap_tf.getText());
   build_matrix();
   collumn_tf.setText(String.valueOf(max_col));
   row_tf.setText(String.valueOf(max_row));
   highest_score_tf.setText(String.valueOf(max_score));
  StringBuilder sb = new StringBuilder();
  for (int i =0; i < rows; i++) {
   for (int j = 0; j < cols; j++) {

          sb.append(String.valueOf(matrix[i][j]));
    sb.append(' ');
    }
}
 matrix_tf.setText(sb.toString());
}                         

Code to build my matrix

     private void build_matrix() {
    String seq1 = sequence1_tf.getText();
    String seq2 = sequence2_tf.getText();



    int r, c, ins, sub, del;

    rows = seq1.length();
    cols = seq2.length();

    matrix = new int [rows][cols];

    // initiate first row
    for (c = 0; c < cols; c++)
        matrix[0][c] = 0;

    // keep track of the maximum score
    max_row = max_col = max_score = 0;

    // calculates the similarity matrix (row-wise)
    for (r = 1; r < rows; r++)
    {
        // initiate first column
        matrix[r][0] = 0;

        for (c = 1; c < cols; c++)
        {
                        sub = matrix[r-1][c-1] +     scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
                        ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));
                        del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));

            // choose the greatest
            matrix[r][c] = max (ins, sub, del, 0);

            if (matrix[r][c] > max_score)
            {
                // keep track of the maximum score
                max_score = matrix[r][c];
                max_row = r; max_col = c;

            }


        }
    }

}

Upvotes: 0

Views: 1642

Answers (1)

Keppil
Keppil

Reputation: 46229

Just append a \n newline after the inner loop:

for (int i =0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        sb.append(String.valueOf(matrix[i][j]));
        sb.append(' ');
    }
    sb.append('\n');
}

Upvotes: 1

Related Questions