【问题标题】:Throwing errors while passing .txt file to class object array将 .txt 文件传递​​给类对象数组时引发错误
【发布时间】:2018-03-16 16:30:46
【问题描述】:

我想要实现的是我有一个“附加课程”文件,其中包含一些格式错误和重复错误。当我将该文件导入我的 Course 对象数组时,它应该会捕获这些错误。我被困在如何检查这些错误上,而且我在导入时遇到了问题。

有人可以看看这两个错误吗?

 public void ImportCourses(string fileName, char Delim)
    {
        FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(stream);
            int index = 0;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var  array = line.Split(Delim);
                Course C = new Course();
                    C.CourseCode = array[0];
                    C.Name = array[1];
                    C.Description = array[2];
                    C.NoOfEvaluations = int.Parse(array[3]);
                    courses[index++] = C;
                    //Console.WriteLine(C.GetInfo()); 

            }
                 reader.Close();
                  stream.Close();

这些是我要检查的例外情况:

  1. 如果字段数不正确,则消息为“记录 {record number} 中的字段数无效”
  2. 如果课程集合中已使用课程代码,则消息为“记录 {record number} 中的课程代码正在使用中”
  3. 如果评估次数不是数字,则消息为“记录 {record number} 中的评估次数格式不正确。

我收到“索引超出范围数组”异常,我不知道从哪里开始。

这是我要导入的 .txt:

【问题讨论】:

  • 你的代码做了很多假设你有正确的数据。
  • 你对“课程”有什么声明?这可能会引发 IndexOutOfRangeException
  • @RaduUngureanu 我有一个名为 Courses 的类,我要导入/导出的类是 CourseManager
  • 是的,放弃我的第一条评论。很可能 IndexOutOfRangeException 来自文件的第 6 行。您只有 3 个用逗号分隔的元素,而在代码中您尝试访问 4 个。
  • @RaduUngureanu 我的 txt 文件中有 4 个元素 CourseCode、CourseName、CourseDescription 和 NoOfEvaluations 你可以查看我提供的链接

标签: c# system.io.file


【解决方案1】:

在尝试访问它们之前,您应该检查 array.Length 以确保您有 4 个元素。如果由于数据为空或数据没有 4 个分隔符而导致拆分失败,则数组不会有 4 个元素长,并且尝试按索引访问不存在的元素将导致 Index out of bounds 异常。

这里有一个解决您问题的可能方法——尽管这听起来像是一个家庭作业问题。

public class Course {
    public string CourseCode { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int NoOfEvaluations { get; set; }
}

List<Course> courses = new List<Course>();

bool CourseAlreadyExists(Course course) { 
    foreach (Course c in courses) { 
        if (c.CourseCode == course.CourseCode) {
            return true;
        }    
    } 

    return false; 
} 

// Define other methods and classes here
public void ImportCourses(string fileName, char Delim) {
    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
        using (var reader = new StreamReader(stream)) {
            int index = 0;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var array = line.Split(Delim);
                if (array.Length != 4)
                {
                    throw new ApplicationException(String.Format("Invalid number of fields in record #{0}", index));
                }

                Course C = new Course();
                C.CourseCode = array[0];
                C.Name = array[1];
                C.Description = array[2];

                int evals;
                if (!int.TryParse(array[3], out evals))
                {
                    throw new ApplicationException(String.Format("Number of evaluations in record {0} is not in correct format.", index));
                }
                else
                {
                    C.NoOfEvaluations = evals;
                }

                if (!CourseAlreadyExists(C))
                {
                    courses[index++] = C;
                }
                else
                {
                    throw new ApplicationException(String.Format("Course code in record {0} is in use", index));
                }
            }
        }
    }
}

}

【讨论】:

  • 他似乎是个十足的初学者。我不确定他会理解 LINQ 部分。但是,是的,这是解决他的问题的方法。
  • 您可以删除那些 .Close(); 行,因为它们在 using-blocks 中。
  • @LarsTech 不错;它们已被删除。添加 using 块时忘记取出它们。
  • @RaduUngureanu 是的,我是初学者,所以我只是用数组来做这个,那么我该如何替换最后一部分?
  • @codechurn 谢谢它帮了很多忙!!我正在学习我不擅长的语言,但我真的很想进入这个领域
猜你喜欢
  • 2016-07-16
  • 2020-05-09
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多