none
Help with my code for an assignment RRS feed

  • Question

  • Yo, working on a C# assignment and my friends and I are stuck. We're making a fake scantron program that uses StreamWriter and StreamReader to make an exam.txt file that we reference to output marks of imaginary students. We try running it and it says that on the

    if(students[key][j]==answers[j])

    The index is outside of the bounds of the array. Where are we going wrong?

    Here is the full code, for reference:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace Project
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (StreamWriter writer =
                new StreamWriter("exam.txt"))
                {
                    writer.WriteLine("BEDEEEACDEDDEEDBBAAD");
                    writer.WriteLine("2956 EBCDEEADDCCXBBAXXDBC");
                    writer.WriteLine("1957 BBAEECAXCCEXEAADADDB");
                    writer.WriteLine("3309 EACECXBDBEEXDBAEBBAX");
                    writer.WriteLine("9573 DEECBABEDAEEAXDEBXDA");
                    writer.WriteLine("4677 AXCEDCXEACDDXECCCEDC");
                    writer.WriteLine("9274 EXECXEXEXCBCAXADDBBB");
                    writer.WriteLine("3746 XBBAEDBCXCEDCEDXXXXA");
                    writer.WriteLine("1966 EXCEBBBEDXXXAXDXXBXE");
                    writer.WriteLine("9922 XXCXACEBACXAAXXEDEDB");
                    writer.WriteLine("2222 DBDAXBXECACBDAEDBBXD");
                    writer.WriteLine("0");
                    writer.Close();
    
    
                }
    
                using (StreamReader reader = new StreamReader("exam.txt"))
                {
                    int correct = 0;
                    string answer;
                    string currentLine;
                    string[] idAnswerPair;
                    answer = reader.ReadLine();
                    Dictionary<string, string> students = new Dictionary<string, string>();
                    int[] answerKey = new int[20];
                    while (!reader.EndOfStream)
                    {
                        currentLine = reader.ReadLine();
                        idAnswerPair = currentLine.Split(' ');
                        students.Add(idAnswerPair[0], idAnswerPair[0]);
    
    
                    }
                    foreach (KeyValuePair<String, String> pair in students)
                    {
                        Console.WriteLine($"{pair.Key}: {pair.Value}");
                    }
    
                    char[] answers = answer.ToCharArray();
                    foreach (string key in students.Keys)
                    {
                        for(int j = 0; j < 20; j++)
                        {
                            if(students[key][j]==answers[j])
                            {
                                correct = correct + 4;
                            }
                            else if(students[key][j] != answers[j])
                            {
                                correct = correct - 1;
                            }
                            else
                            {
                                correct = correct + 0;
                            }
                        }
                    }
                    /*foreach (char a in answers)
                    {
                        for(int i = 0; student1.Equals(answer); i++)
                        {
                            if(student1 == "X")
                            {
                                correct = correct + 0;
                            }
                            else if(student1 != answer)
                            {
                                correct = correct - 1;
                            }
                            correct = correct + 1;
                        }
                        Console.WriteLine(a);
                    }*/
                    Console.WriteLine(students);
                }
                Console.ReadKey();
            }
        }
    }
    

    Any help would be appreciated in solving the problem! Thanks!

    Friday, August 10, 2018 12:34 AM

All replies

  • Greetings Austin. When you add an entry to the students dictionary, you are setting the value of each entry to the key. But the key is only 4 characters long, so as soon as you try to read the 5th character, you get an out of range exception. The value should be the 20 character string of the student's answers.

                while (!reader.EndOfStream)
                {
                   currentLine = reader.ReadLine();
                   idAnswerPair = currentLine.Split(' ');
    
                   if (idAnswerPair.Length > 1) // You need this condition to prevent a crash on the last line ("0").
                   {
                      students.Add(idAnswerPair[0], idAnswerPair[1]); // Key is [0], value is [1].
                   }
    
    
                }



    • Edited by Ante Meridian Friday, August 10, 2018 1:12 AM Formatting was stuffed.
    Friday, August 10, 2018 1:10 AM
  • Awesome, it worked, thank you so much!

    When it runs it does display the ID and answers of each student, but at the bottom it also displays System.Collections.Generic.Dictionary`2[System.String,System.String]. Why is it showing this, and what can I do to fix it?

    Friday, August 10, 2018 1:43 AM
  • That's because you are doing Console.WriteLine(students). This means you are trying to write a single string that represents the dictionary, and by default you get that longwinded definition of what the dictionary is. If you want to print each student, you probably want something like the following.

                //Console.WriteLine(students);
                foreach (KeyValuePair<string, string> student in students)
                {
                   Console.WriteLine(student.Key + " " + student.Value);
                }

    You probably don't want exactly that, because it just tells you the numbers and their answers, which you are already printing. But something along those lines.

    Friday, August 10, 2018 1:54 AM