-1

I'm trying to read the following textfile:(skipping first 8 lines) And reading from arrow each column enter image description here

And am doing so by putting each column value in an array which is dictated by position and length

To test if the array value actually captured a column value I want to see the value[0] when I click another button. But when I run my app, I get the error that my index was out of bounds of the array? How, when my array size is 3 and I don't go beyond that.

    string[] val = new string[3 ]; // One of the 3 arrays - this stores column values

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

            string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
            textBox1.Lines = lines;

             int[] pos = new int[3] { 3, 6,18}; //setlen&pos to read specific clmn vals
             int[] len = new int[3] {2, 10,28}; // only doing 3 columns right now



             foreach (string line in textBox1.Lines)
             {
                 for (int j = 0; j <= 3; j++)
                 {
                     val[j] = line.Substring(pos[j], len[j]); // THIS IS WHERE PROBLEM OCCURS
                 }

             }

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {   // Now this is where I am testing to see what actual value is stored in my    //value array by simply making it show up when I click the button.

        MessageBox.Show(val[0]);
    }
}

}

user2788405
  • 195
  • 2
  • 5
  • 16
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Liam Sep 28 '20 at 09:46

3 Answers3

2

arrays are 0 indexed, that means that an array with 3 elements will have elements at indexes 0, 1, and 2.

3 is out of bounds, so when you try to access pos[3] or len[3], your program will thrown an exception.

use j < 3 instead of j<=3

 for (int j = 0; j < 3; j++)
 {
     val[j] = line.Substring(pos[j], len[j]); // THIS IS WHERE PROBLEM OCCURS
 }
2

The problem is that you go all the way up to j == 3 in the forstatement. This will be the fourth element since arrays are zero-based, so change the for statement to:

for (int j = 0; j < 3; j++)

and you will be good to go.

JLe
  • 2,835
  • 3
  • 17
  • 30
2

The array pos has three values in it.

Consider your for loop.

  1. First i is zero. That is less than or equal to three.
  2. Then i is one. That is less than or equal to three.
  3. Then i is two. That is less than or equal to three.
  4. Then i is three. That is less than or equal to three.
  5. Then i is four. That is not less than or equal to three.

It executes the body of the loop 4 times. There are 3 items.

For the fix, to follow standard conventions, just use a less then, rather than a less than or equal to, in the condition check of your for loop.

Servy
  • 202,030
  • 26
  • 332
  • 449