2015-10-10 4 views
-2

Я занимался своей работой в 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; 
    } 
} 

Бизнес Layer

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

Я буду благодарен, если вы можете помочь мне с этой проблемой

+0

Необработанное исключение: указатель был на границе массива. на Business.Fill_data, строка 31; в Interface.output, строка 79; Program.Main. line 19. И бесконечный цикл, который означает, что он печатает «Неверные данные» и «не удался» каждый раз, когда я нажимаю enter. – Cassie

+0

Вы можете увидеть это на изображении – Cassie

+0

Теперь это то, что я называю _Remote Debugging_. –

ответ

1

Возможно, в вашем бизнес-слой на линии 31 есть ошибка:

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

Вы должны проверить эту строку, вероятно, нет filed_value [1]. Просто filed_value [0] и все.

Смежные вопросы