1

I have some problems with the code every time I try to compile the exception java.lang.StringIndexOutOfBoundsException appears. Here is the code with the problem I really don't know what I have done wrong. In the code I try to split a string using some conditions, the string represent a polynomial.

int[] coef1= new int[20];
for(i=0;i<polinom.length()+1;i++){
    if(polinom.charAt(i)=='+' )
         c=polinom.charAt(i+1);
    else{
       if(polinom.charAt(i)=='^'){
            v=Integer.parseInt(Character.toString(polinom.charAt(i+1)));
            coef1[v]=Integer.parseInt(Character.toString(c));
            System.out.print(coef1[v]);

       }
    }
}
for(i=0;i<polinom.length()+1;i++){
    if(polinom.charAt(i)=='-' )
         c=polinom.charAt(i+1);
    else{
       if(polinom.charAt(i)=='^'){
            v=Integer.parseInt(Character.toString(polinom.charAt(i+1)));
            coef1[v]=-Integer.parseInt(Character.toString(c));
            System.out.print(coef1[v]);

        }
    }
}

The exception is here if(polinom.charAt(i)=='+' )

7
  • 3
    post the stack trace would be much easier to track where exception was thrown Commented Mar 3, 2016 at 14:19
  • I agree with @Ramanlfc this error can be rather easy to resolve if we know the line it is occurring at. Commented Mar 3, 2016 at 14:20
  • 3
    In the second line here you are using for(i=0;i<polinom.length()+1;i++){. That +1 should be -1. Commented Mar 3, 2016 at 14:20
  • for(i=0;i<polinom.length()+1;i++) you are running loop with length + 1. Commented Mar 3, 2016 at 14:21
  • The problem is that nothing works I changed the +1 with -1 but the exception still appears Commented Mar 3, 2016 at 14:28

3 Answers 3

4

Just replace all your

for(i=0;i<polinom.length()+1;i++){

with

for(i=0;i<polinom.length()-1;i++){

As indices are 0-based and you use polinom.charAt(i+1), i+1 should never be equal (nor greater) than polinom.length.

Or if you want ot be able to test until the last character of you string (for other processing), you can ensure that polinom.charAt(i+1) gets never triggered if i == polinom.length() - 1, just add a test before processing your stuff:

for(i=0;i<polinom.length();i++){ // not using -1, looping to the end of the string
    if(polinom.charAt(i)=='+' && i < polinom.length() - 1) // checking that no exception will be thrown
        c=polinom.charAt(i+1);
    else{
       if(polinom.charAt(i)=='^' && i < polinom.length() - 1){ // same
           v=Integer.parseInt(Character.toString(polinom.charAt(i+1)));
           coef1[v]=-Integer.parseInt(Character.toString(c));
           System.out.print(coef1[v]);
        }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the second line here you are using

for(i=0;i<polinom.length()+1;i++){

That +1 should be -1.

Comments

0

I suppose the variable polinom is a String.

Your're looping beyond the end of the string:

for(i=0;i<polinom.length()+1;i++)

It should be

for(i=0;i<polinom.length()-1;i++)

3 Comments

Your answer is the same as X.L.Ant, if you have nothing new, so no need to copy paste, I think it is better to have something new.
@BahramdunAdil: you're right but I saw that answer only after having posted mine.
OK, It was just a suggestion to have a unique answer.

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.