1

Hi I am in a bit of an issue I keep getting this error:

Index was outside the bounds of the array.

var d = File.ReadAllLines(@"studentsFile.txt");
var t = d.Where(g => g.Contains("Student Name"));
string[] splited;
foreach (var item in t)
{
    splited = item.Split(new string[] { "Student Name:" }, StringSplitOptions.None);
    cbListStudents.Items.Add(splited[1]);
}

The above works perfectly but the code below does not:

var cour = File.ReadAllLines(@"CourseFile.txt");
var courFind = cour.Where(g => g.Contains("Course"));
string[] splited2;
foreach (var item in courFind)
{
    splited2 = item.Split(new string[] { "Course:" }, StringSplitOptions.None);
    cbListCourses.Items.Add(splited2[1]);//here is where the issues starts
}
MatSnow
  • 7,357
  • 3
  • 19
  • 31
Ray
  • 21
  • 2
  • 1
    Either: You have a line which when split leaves < 2 items, or you have a blank line in your file. Why not use `StartsWith(...)` and `Substring(...)` for this task? Anyway, the absolute 100% best way to diagnose these issues without posting questions to random Internet strangers is to use the debugger and inspect the values yourself. – ProgrammingLlama Nov 21 '17 at 07:04
  • 1
    Possible duplicate of [Error : Index was outside the bounds of the array.](https://stackoverflow.com/questions/21700879/error-index-was-outside-the-bounds-of-the-array) – ProgrammingLlama Nov 21 '17 at 07:06
  • 1
    You do realise that array indexes start at 0 for the first item? – oerkelens Nov 21 '17 at 07:13
  • 1
    error is self explanatory - `item.Split(new string[] { "Course:" }` resulted in empty or single element array, try to check on array size before accessing it – Vladimir Nov 21 '17 at 07:16
  • 1
    You search for `Course` and then split on `Course:` you probably are trying to split strings that do not contain `Course:` (with a colon at the end). – heijp06 Nov 21 '17 at 07:35

4 Answers4

0

At least you should check the array length:

var cour = File.ReadAllLines(@"CourseFile.txt");
var courFind = cour.Where(g => g.Contains("Course"));
string[] splited2;
foreach (var item in courFind)
{
  splited2 = item.Split(new string[] { "Course:" }, StringSplitOptions.None);
  if(splited2.Length >= 2)
    cbListCourses.Items.Add(splited2[1]);//here is where the issues starts
}
KBO
  • 653
  • 7
  • 17
0
foreach (var item in courFind)
{
   splited2 = item.Split(new string[] { "Course:" }, StringSplitOptions.None);
   cbListCourses.Items.Add(splited2[0]); // Array Index starts with 0
}
Vinoth Raj
  • 296
  • 1
  • 14
0

You have to check array's Length (what if the file has, say, empty lines?)

var data = File
  .ReadLines(@"CourseFile.txt")
  .Select(line => new string[] { "Course:" }, StringSplitOptions.None)
  .Where(items => item.Length >= 2) // Check array's Length
  .Select(items => items[1])        // Now it's safe to address the 2nd item  
  .ToArray();                       // ComboBox.Items.AddRange wants array

cbListCourses.Items.AddRange(data);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

The file might not contain any colon character (:) in the line.

You should check as if

var courFind = cour.Where(g => g.Contains("Course:"));

instead of the previous.

Test cases followed:

  1. File with Empty content
  2. with content -> Course:
  3. with content -> Course (Error same as you faced)

Code:

 var cbListStudents = new List<String> ();
 var cbListCourses = new List<String> ();

 var d = File.ReadAllLines (@"res/TestFile.txt");
 var t = d.Where (g => g.Contains ("Student Name"));
 string[] splited;
 foreach (var item in t) {
    splited = item.Split (new string[] { "Student Name:" }, StringSplitOptions.None);
    cbListStudents.Add (splited[1]);
 }

 var cour = File.ReadAllLines (@"res/TestFile2.txt");
 var courFind = cour.Where (g => g.Contains ("Course"));
 string[] splited2;
 foreach (var item in courFind) {
    splited2 = item.Split (new string[] { "Course:" }, StringSplitOptions.None);
    cbListCourses.Add (splited2[1]); //here is where the issues starts
 }        
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43