1

I want to increment (using a counter) the value of a C# array. However I always get an error:

Index was outside the bounds of the array.

Here is my code.

while ((line = s.ReadLine()) != null)
{

    string[] parts = new string[40];
    parts=line.Split(' ');
    int a;
    for (a = 0; a <= (parts.Length - 1); a++)
    {

        if (parts[a] == "if")
        {
            node = node + 1;
            edge = edge + 1;
            int b = a + 2;
            Console.Write(parts[b]);
            if ((parts[a + 2]) == "{")
            {
                node = node + 1;
            } 
        }
    }
}
3
  • On which line? Did you debug your code? ericlippert.com/2014/03/05/how-to-debug-small-programs You can find your problem easily with debugging. Commented Apr 26, 2014 at 12:38
  • is it on this line? 'if ((parts[a + 2]) == "{")' ? Commented Apr 26, 2014 at 12:40
  • new string[40] is a waste of time and memory because the next statement assigns a new value. Just use var part = line.Split(' '); Commented Apr 26, 2014 at 13:02

4 Answers 4

5

The problem is parts[a + 2] when you reached to the end a+2 is out of bounds of array

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

Comments

1

Did you check that

parts[a + 2] doesn't exceed the array length?

One solution could be as follows:

while ((line = s.ReadLine()) != null)
{

    string[] parts = new string[40];
    parts=line.Split(' ');
    int a;
    for (a = 0; a <= (parts.Length - 1); a++)
    {

        if (parts[a] == "if")
        {
            node = node + 1;
            edge = edge + 1;
            int b = a + 2;
            Console.Write(parts[b]);
            if (((a + 2) < parts.length) && (parts[a + 2]) == "{")
            {
                node = node + 1;
            } 
        }
    }
}

In the code an extra check is put to see if a + 2 doesn't exceed the length of the parts-array. Then, the check is done, if the contents at array index a + 2 is equal to "{". If both conditions are true, then the code inside the block is evaluated.

Comments

0

If you use parts[a+2] you can only do the for loop until parts.Lenth -2:

while ((line = s.ReadLine()) != null)
{

    string[] parts = new string[40];
    parts=line.Split(' ');
    int a;
    for (a = 0; a <= (parts.Length - 2); a++)
    {

        if (parts[a] == "if")
        {
            node = node + 1;
            edge = edge + 1;
            int b = a + 2;
            Console.Write(parts[b]);
            if ((parts[a + 2]) == "{")
            {
                node = node + 1;
            } 
        }
    }
}

Comments

0

Your problem is here

int b = a + 2;
Console.Write(parts[b]); // here is the first problem
if ((parts[a + 2]) == "{") // why do a+2 here when you know parts[b] is the same thing (a +2)
{
      node = node + 1;
} 

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.