无限循环和未处理异常时出错

时间:2015-10-10 18:51:19

标签: c# loops infinite-loop unhandled-exception multi-layer

我在c#做我的实验。我有这个错误。无法弄清楚如何处理它。 我的代码包含多个层次 - 学生班级

----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

public struct Student{
    public string Name;
    public string Surname;
    public int Year;
    public int ID;
    public int Result;
    public string Country;
    public string Gradebook;
}

数据层

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

public class Data{
    public string Name;

    public Data()
    {

    }

    public void Create_file(string Name, Student []st)
    {
        FileStream new_file = new FileStream(Name + ".txt", FileMode.Create);

        for (int i = 0; i < st.Length; i++)
        {
            string write_st = st[i].Name + st[i].Surname + st[i].Year + st[i].Country + st[i].ID + st[i].Result;
            byte[] array = Encoding.Default.GetBytes(write_st);
            new_file.Write(array,0,array.Length);
            new_file.Flush();
        }
        new_file.Close();
    }

    public string Open_file(string Name)
    {
        FileStream new_file = File.OpenRead(Name +".txt");

        byte[] array = new byte[new_file.Length];
        new_file.Read(array,0,array.Length);
        string ReadText = Encoding.Default.GetString(array);

        return ReadText;
    }
}

业务层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Business{
    public Data d1 = new Data();
    Student[] st;
    public string file_name;
    public string get_data;

    public Business(string file_name, Student [] s)
    {
        d1.Create_file(file_name, s);
    }

    public void Fill_data(string file_name)
    {
        get_data = d1.Open_file(file_name);
        string[] separators = new string[] {"\r\n"}; 
        string[] lines = get_data.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        st = new Student [lines.Length];

        for (int i = 0; i < lines.Length; i++)
        {
            string [] filed_value = lines[i].Split(' ');
            st[i] = new Student();
            st[i].Name = filed_value[0];
            st[i].Surname = filed_value[1];
            st[i].Year = Convert.ToInt32(filed_value[2]);
            st[i].ID = Convert.ToInt32(filed_value[3]);
            st[i].Country = filed_value[4];
            st[i].Result = Convert.ToInt32(filed_value[5]);
            st[i].Gradebook = filed_value[6];
        }
    }

    public int Find_students()
    {
        int count = 0;

        for (int i = 0; i < st.Length; i++)
        {
            if(st[i].Year == 3 && st[i].Country == "Ukraine")
            {
                count++;
                Console.WriteLine(st[i].Name + " " + st[i].Surname);
            }
        }

        return count;
    }

用户界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;


class Interface{
    Data d2 = new Data();
    public Student[] st;
    Business b1;
    public string new_filename;
    const string format = "{0,2}{1,10}{2,10}{3,8}{4,12}{5,12}{6,15}";
    string[] pattern = new string[] { @"^[a-zA-Z]+$", @"^[a-zA-Z]+$", @"\d+", @"\d+", "^([0-9]{1})$", @"\d+" ,@"^([0-9]{6})$" };

    public Interface()
    {

    }

    public void input(int number, string new_filename1)
    {
        st = new Student[number];
        string line = string.Format(format, "Name","Surname","Year","ID","Country","Result","Gradebook");
        Console.WriteLine(line);
        Console.WriteLine();

        //string Inp = Console.ReadLine();
        //string[] FileValue = Inp.Split(' ');

        for (int i = 0; i < st.Length; i++)
        {
            //Console.WriteLine(i + 1);
            string new_line = Console.ReadLine();
            string [] filed_value = new_line.Split(' ');

            if (Check(filed_value) == true)
            {
                st[i] = new Student();
                st[i].Name = filed_value[0];
                st[i].Surname = filed_value[1];
                st[i].Year = Convert.ToInt32(filed_value[2]);
                st[i].ID = Convert.ToInt32(filed_value[3]);
                st[i].Country = filed_value[4];
                st[i].Result = Convert.ToInt32(filed_value[5]);
                st[i].Gradebook = filed_value[6];
            }

            else
            {
                //i--;
                Console.WriteLine("failed");
            }
        }

        new_filename = new_filename1;
        b1 = new Business(new_filename,st);

    }

    public bool Check(string [] filed_value)
    {
        for(int j = 0; j < filed_value.Length ; j++)
        {
            Match match = Regex.Match(filed_value[j],pattern[j]);

            if(!match.Success)
            {
                Console.WriteLine("Wrong data" + " " + filed_value[j]);
                return false; 
            }  
        }
        return true;
    }

    public void output()
    {
        b1.Fill_data(this.new_filename);
        Console.WriteLine("The found students number is" + b1.Find_students());
    }

}

主要

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_2_layers_C_sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Interface new_I = new Interface();
            Console.WriteLine("Enter quantity of students");
            int count = Convert.ToInt32(Console.ReadLine()); 
            Console.WriteLine("Enter the name of the file");
            string Name = Console.ReadLine();
            new_I.input(count,Name);
            new_I.output();

            Console.ReadLine();
        }
    }
}

大多数错误都在用户界面层中。我得到的结果可以在屏幕上看到:

enter image description here

enter image description here

如果你能解决这个问题,我将不胜感激

1 个答案:

答案 0 :(得分:1)

可能在第31行的商业图层中存在错误:

st[i].Surname = filed_value[1];

你需要检查这个字符串,可能没有filed_value [1]。只是filed_value [0],那就是它。