-2

I was doing my labwork in c#. And I have got this mistake. Can not figure out how to deal with it. My code consists of several layers - Student class

----------
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;
}

Data layer

   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;
    }
}

Business 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;
    }

User Interface

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());
    }

}

And main

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();
        }
    }
}

Most of the mistakes are in the user interface layer. The result I get you can see in the screens:

enter image description here

enter image description here

I will be grateful, if you can help me with this problem

Slyvain
  • 1,732
  • 3
  • 23
  • 27
Cassie
  • 2,941
  • 8
  • 44
  • 92
  • Unhandled Exception: Index was otside the bounds of array. at Business.Fill_data, line 31; at Interface.output, line 79; Program.Main. line 19. And infinite loop which means it prints "Wrong data" and "failed" every time I press enter. – Cassie Oct 10 '15 at 19:08
  • You can see it in the image description – Cassie Oct 10 '15 at 19:09
  • Now this is what I call _Remote Debugging_. – Uwe Keim Oct 10 '15 at 19:20
  • @UweKeim indeed, Cassie trust me you need 5 min with someone who knows how to program so they can guide you. Way to much in here which needs to change, even if you get your PROBLEM solved. You are making this so much hard than you need to. You are confusing basically structuring... which is making the code make my eyes bleed. You have the layers but consuming them wrong. – Seabizkit Oct 10 '15 at 19:30
  • This is not an infinite loop. It's an index out of range error. This is easy to determine the cause if you simply debug your code and step through it. – Steve Oct 10 '15 at 19:35
  • ok, thank you. I`ll try to rewrite it and make it better – Cassie Oct 10 '15 at 19:52
  • Looks like a project I wrote in college with punch cards. Teacher gave an input file full of errors and had to keep modifying the code until I got through entire input file. Program ended up with over 500 punch cards (more in the circular basket than in the final deck). Lesson learned : When writing code make sure you consider error paths which you don't have. Humans make plenty of typos. – jdweng Oct 10 '15 at 20:06

1 Answers1

1

Probably in your business layer on line 31 there is an error:

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

You need to check this string, probably there is no filed_value[1]. Just filed_value[0] and that's it.

Anton Norko
  • 2,166
  • 1
  • 15
  • 20