0

Hello guys here is my code

    for (int i = 0; i <= alignedSeqA.length(); i++) {


            if(alignedSeqA.charAt(i)==alignedSeqB.charAt(i)) {

                alignedSeqPenalty +="0";  
            }
            else if(alignedSeqA.charAt(i)=='-'){
                alignedSeqPenalty +="2";


            }else if(alignedSeqB.charAt(i)=='-'){

                alignedSeqPenalty +="2";

            }else if(alignedSeqA.charAt(i)!=alignedSeqB.charAt(i)){

                alignedSeqPenalty +="1";

            }

            }

and here is my error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
    at java.lang.String.charAt(Unknown Source)
    at New.main(New.java:124)

when i changed my alignedSeqA.length() with an integer (like 7) it works just fine

i.e. --> output when i change it to 7 20100201

what am i doing wrong?

Thank you

1
  • Please don't deface your questions when they have been solved... Just accept the proper answer and let them be. Commented Dec 20, 2013 at 9:14

3 Answers 3

2

You need to use:

for (int i = 0; i < alignedSeqA.length(); i++) {

As first index is 0 and last is alignedSeqA.length() - 1

Sign up to request clarification or add additional context in comments.

1 Comment

oh this is what happens when you eat for 16 hours how did i miss that thank you :)
1

In Java (and most programming languages), arrays are zero-based.

i <= alignedSeqA.length() 

Should be

i < alignedSeqA.length()
  ↑

Meaning that if you have an array of size N, the indexes will be from 0 to N - 1 (total sum will be N).

To better explain it, let's take a specific example. Say alignedSeqA is of size 5, it looks like this:

  0   1   2   3   4
+-------------------+
|   |   |   |   |   |
+-------------------+

So if you loop until (include) the size (which is 5), you're out of bounds.

Comments

0

The issue is here:

  i <= alignedSeqA.length();
     |
Remove this `=` from the for loop condition

It should be like this:

i < alignedSeqA.length();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.