Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:58:33+00:00 2026-06-13T10:58:33+00:00

I am trying to build a matrix with row and column having values such

  • 0

I am trying to build a matrix with row and column having values such as “aaa” for aligning purposes. but when I run it I get an error. below is my code

public class compute_matrix {
  static String seq1="aaa";
  static String seq2="aaa";
  static int[][] matrix;
  static int max_row;
  static int max_col;
  private static int match_reward=1;
  private static int mismatch_penalty= -1;
  private static int gap_cost= -1;
  private static boolean case_sensitive;

  private static boolean isCaseSensitive() {
    return case_sensitive;
  }

  private static int max(int ins, int sub, int del, int i) {
    if (ins > sub) {
        if (ins > del) {
            return ins > i? ins : i;
        } else {
            return del > i ?del : i;
        }
    } else if (sub > del) {
        return sub> i ? sub : i;
    } else {
        return del > i ? del : i;
    }
}

 protected char sequence[];

   public static void main(String args[]){
    int r, c, rows, cols, ins, sub, del, max_score;

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

    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;
            }
        }
    }
     }

private static int scoreSubstitution(char a, char b) {
   if (isCaseSensitive())
        if (a == b)
            return match_reward;
        else
            return mismatch_penalty;
    else
        if (Character.toLowerCase(a) == Character.toLowerCase(b))
            return match_reward;
        else
            return mismatch_penalty;
}

private static int scoreInsertion(char a) {
 return gap_cost;
}

private static int scoreDeletion(char a) {
    return gap_cost;
}
public char charAt (int pos)
{
    // convert from one-based to zero-based index
    return sequence[pos-1];
}

  }

and my error is displaying this

              Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(String.java:695)
    at compute_matrix.main(compute_matrix.java:67)

Java Result: 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:58:33+00:00Added an answer on June 13, 2026 at 10:58 am
       rows = seq1.length()+1;
        cols = seq2.length()+1;
    
        matrix = new int [rows][cols];
    

    and then later:

       for (c = 1; c < cols; c++)
        {
       //when c == cols-1, it is also `seq2.length()`
       //the access to seq2.charAt(c) will cause this exception then.
                        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));
    

    In the above loop, when c == cols-1, it is also seq2.length(), the access to seq2.charAt(c) will cause this exception then.

    You initialize the number of rows and cols to length() + 1, while you later iterate from 0 to length (inclusive), while the string contain only length() chars – from 0 to n exclusive.

    If you are a C programmer in your past – I assume you are expecting a \0 terminator at the end of the string. In java you don’t have those – since String is an object – you can hold a field to indicate its exact length. Meaning the last char in the string, is actually the last character there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I
Trying to build my project with ANT in idea 10 and I get a
I'm trying to build an OCR application on Android using Tesseract, but when I
Trying to build a Metro app using Javascript and having issues with IndexedDb. I
I'm trying to build a matrix form by creating a uitable in GUIDE. So
I've run into a frustrating problem while trying to build a grid of colors
I am trying to build square band matrices using blkdiag or spdiags , but
I am trying to build a three dimensional matrix out of three vectors where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.