-3

when i run my code and enter an input my code throws an exception index outside bounds of array here is my code

public static void ShowDiscription()
        {
            Console.WriteLine("Enter Course ID: ");
            string ReqCourseID = Console.ReadLine();
            Console.WriteLine();
            if (Program.AllCourses.ContainsKey(ReqCourseID))
            {
                FileStream FS = new FileStream("Description.txt", FileMode.Open);
                StreamReader SR = new StreamReader(FS);

                while (SR.Peek() != -1)
                {
                    string z = SR.ReadLine();


                    String[] Fields;
                    Fields = z.Split('@');
                    string courseName = Fields[0];
                    string coursedescription = Fields[1];
                    if (ReqCourseID.CompareTo(Fields[0]) == 0)
                    {

                        Console.WriteLine(Fields[1]);
                        SR.Close();
                        return;
                    }

                }

            }
          else { 
            Console.WriteLine("Entered Course ID is not found! press any key to continue");

                }

        }

i dont know whats the problem the file has a delimter @ which divides the subject code and its description. does it differ if the description.txt file is too large? Regards

J.Doe
  • 1
  • 5
  • 1
    Use breakpoints and step through your code and find out what is different than expected. – 404 Dec 27 '16 at 22:36
  • no thats not me, and thats c# not java lol @GeoffOverfield – J.Doe Dec 27 '16 at 22:36
  • I'm aware of the difference in programming languages. I marked it as a duplicate because an index out of bounds is the same in any language. I write in 6 languages, and arrays are always fundamentally the same. _And it doesn't have to be your post to be a duplicate..._ Regardless, check the answer below. – Uchiha Itachi Dec 27 '16 at 22:44
  • @GeoffOverfield Flagging a question for closure and then *answering that question* sends mixed signals. Closing a question prevents others from answering - so you shouldn't post an answer either. – Rob Dec 27 '16 at 22:50
  • Well thanks Rob. Good to know the Signal Police are with us today. I flagged it because *IT IS* a duplicated. There's probably 500 posts you could find in an instant about indexes being out of bounds on Stack Overflow, much less Google... Obviously he wasn't going to go looking for them, so I still gave him an answer. Next time, I'll check with you to make sure that's ok. Sound good? – Uchiha Itachi Dec 27 '16 at 22:59

1 Answers1

0

You never initialize your array.

int count;   //number of indices that your array will contain.
string [] Fields =  new string[count];

If you don't know how many indices you will have, use a List<string> instead:

List<string> Fields =  new List<string>();
string courseName = "Programming";
Fields.Add(courseName);
Uchiha Itachi
  • 1,251
  • 1
  • 16
  • 42
  • added string [] Fields = new string[2]; and same error – J.Doe Dec 27 '16 at 22:43
  • put in a break point and see where it goes out of bounds. – Uchiha Itachi Dec 27 '16 at 22:46
  • 1
    You can initialize it with any value you like, but string.Split returns an array and the code assign the return of string split to the array variable, so the initialized dimension is lost. The problem is: one of the lines has not two fields. Perhaps the last line is an empty line (happens a lot) – Steve Dec 27 '16 at 22:57