-1

私は大きな進歩を遂げましたが、ファイルの読み込みに問題があります。ファイル読み込みメソッドを持つクラスは次のとおりです。

public class BunchOfDeliverables
{
    private List<Person> myPersons;
    private List<Deliverable> myDeliverables;

    public BunchOfDeliverables()
    {
        this.myPersons = new List<Person>();
        this.myDeliverables = new List<Deliverable>();
    }

    public List<Person> Persons { get { return this.myPersons; } }
    public List<Deliverable> Deliverables { get { return this.myDeliverables; } }

    public void LoadPersonsFromFile(String filename)
    {
        StreamReader sr = null;
        try
        {
            sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
            String name, street, housenr, postalcode, city;
            name = sr.ReadLine();
            while (name != null)
            {
                street = sr.ReadLine();
                housenr = sr.ReadLine();
                postalcode = sr.ReadLine();
                city = sr.ReadLine();
                this.myPersons.Add(new Person(name, street, Convert.ToInt32(housenr), postalcode, city));
                name = sr.ReadLine();
                name = sr.ReadLine(); //and again read a line, because of the delimiter (line with the stars)
            }
        }
        catch (IOException) { }
        finally
        {
            if (sr != null) sr.Close();
        }
    }

    public void LoadDeliverablesFromFile(String filename)
    {
        StreamReader sr = null;
        try
        {
            sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
            String s;
            s = sr.ReadLine();
            while (s != null)
            {
                String[] items = s.Split();
                this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));
                s = sr.ReadLine();
            }
        }
        catch (IOException) { }
        finally
        {
            if (sr != null) sr.Close();
        }
    }


    public void AddPerson(Person p)
    {
        this.myPersons.Add(p);
    }


    public Deliverable FindDeliverable(int id)
    {
        foreach (Deliverable d in this.myDeliverables)
        {
            if (d.ID == id)
            {
                return d;
            }
        }
        return null;
    }

    public void AddDeliverable(Deliverable d)
    {
        if (FindDeliverable(d.ID) == null)
        {
            myDeliverables.Add(d);
        }
        else
        {
            throw new Exception("Be aware: nothing is added!!!");
        }

    }

そしてここに - 私がそれらを初期化した形式で:

public partial class Form1 : Form
{
    BunchOfDeliverables d;
    public Form1()
    {
        InitializeComponent();
        d = new BunchOfDeliverables();
        d.LoadDeliverablesFromFile("../../data/deliverables.txt");
        d.LoadPersonsFromFile("../../data/persons.txt");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        foreach (Person per in d.Persons)
        {

            listBox1.Items.Add(per);
        }


    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void ViewDeliv_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        foreach (Deliverable deliv in d.Deliverables)
        {

            listBox1.Items.Add(deliv);
        }
    }

今、Persons の読み込みファイルの場合、それは魅力のように機能し、何も問題はありませんが、Deliverables ファイルを読み込もうとすると、次のエラーが発生します。

インデックスが範囲外でした。負ではなく、コレクションのサイズより小さくなければなりません。この行のパラメーター名: index":

"this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));"

成果物ファイルが小さな例でどのように構成されているか (空行なし) は次のとおりです。

1 350 1
2 700 5
3 360 7
4 360 6

どんな助けでも大歓迎です!

4

2 に答える 2

0

このコード:

String[] items = s.Split();
this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));

は、入力ファイルのすべての行に、スペースで区切られた項目が少なくとも 3 つあると想定しています。

行に含まれるアイテムが少ない場合、このエラーが発生します。これは、入力ファイルにエラーがあることを意味するか、解析で考慮されていない有効なデータがファイルにあることを意味する可能性があります。

十分なデータがない行を単純にスキップするには、次のようにします。

String[] items = s.Split();
if (items.Count >= 3)
    this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));

ただし、それが有効かどうかは、エラーがデータ ファイルにあるか、またはそのデータの予想される形式にあるかによって異なります。データにエラーがある場合は、実際にエラー メッセージを表示する必要があります。

于 2013-05-11T09:35:47.163 に答える
0

このエラーは、配列itemsが要素を短くする必要があることを意味します。したがって、要素は 3 ではなく、0、1、または 2 つしかありません。

おそらく、ファイルの最後に情報が含まれていない空の行があります。

while ループを次のように変更してみてください。

while(!string.IsNullOrWhiteSpace(s))
    ....

配列内の項目の数が 3 に等しいかどうかを最初に確認することもできます。

それでも解決しない場合は、その行にブレークポイントを設定してください。コードが壊れるたびに、 と の値を確認してsくださいitems

于 2013-05-11T09:32:13.073 に答える