簡體   English   中英

解析文本文件時出錯

[英]Error in parsing text file

大家好,我的代碼有錯,我不明白為什么,這是我的代碼:

private void Parsing_String(string filename)
    {
        int outValue;
        int[][] number = new int[26][];
        List<Row> list = new List<Row>(); 

        //StreamReader freader = File.OpenText(filename);

        var parsed = File.ReadLines(filename)
            .Select(line => line.Split(' ')
                .Where(IsInteger)
                .Select(s => int.Parse(s))
                .ToArray())
            .ToArray();

        foreach (String str in File.ReadLines(filename))
        {
            String[] strCols = str.Split(' ');

            /*for (int i = 0; i < 26; i++)
            {
                number[i] = new int[strCols.Length];
                for (int j = 0; j < strCols.Length; j++)
                {
                    number[i][j] = int.TryParse(strCols[j].Substring(2), out outValue) ? outValue : 0;
                    listBox2.Items.Add(number[i][j]);
                }
            }*/


            list.Add(new Row()
            {
                Column1 = int.TryParse(strCols[0].Substring(2), out outValue) ? outValue : 0,
                Column2 = int.TryParse(strCols[1].Substring(2), out outValue) ? outValue : 0,
                Column3 = int.TryParse(strCols[2].Substring(2), out outValue) ? outValue : 0,
                Column4 = int.TryParse(strCols[3].Substring(2), out outValue) ? outValue : 0,
                Column5 = int.TryParse(strCols[4].Substring(2), out outValue) ? outValue : 0,
                Column6 = int.TryParse(strCols[5].Substring(2), out outValue) ? outValue : 0,
            });

        }

        dg.ItemsSource = list;


        label3.Content = number[1][0];
        label4.Content = number[0][1];
        int kali = number[0][0] * number[0][1];
        label2.Content = kali;
    }

    static bool IsInteger(string possibleInt)
    {
        int k;
        return int.TryParse(possibleInt, out k) ? k : 0;        
    }


    public class Row
    {
        public int Column1 { get; set; }
        public int Column2 { get; set; }
        public int Column3 { get; set; }
        public int Column4 { get; set; }
        public int Column5 { get; set; }
        public int Column6 { get; set; }
    }

    #endregion


}

} //這里文本文件樣本10192 20351 30473 40499 50449 60234 10192 20207 30206 40203 50205 60226 10192 20252 30312 40376 50334 60252 10192 20271 30332 40405 50209 60234

誰能告訴我我做錯了什么?

我想要做的是,我有一個包含數字的文本文件,我想解析它,並將每個數字放在一個數組中,以便我可以輕松訪問它。 我的代碼是否足夠有效?

謝謝,很多建議。

你的主要錯誤似乎是:

int[][] number = new int[26][];

這只是實例化一個數組數組,而不是嵌套數組本身,所以當你寫

number[i][j] = ...

number[i]返回null,並且在null上調用indexer是NullReferenceException!

編輯:
你應該寫的是:

for (int i = 0; i < 26; i++)
{
  number[i] = new int[strCols.Length];
  for (int j = 0; j < strCols.Length; j++)
  {
      //...
  }
}

在我看來,如果沒有數組初始化,這會更容易(如果您將此作為在C#中使用數組的練習,請跳過此答案)。

var parsed = File.ReadLines("SO.txt")
                 .Select(line => line.Split(' ') 
                                     .Select(MyIntegerParse)  // pick out each item as an int
                                     .ToArray())  // get array of ints 
                 .ToArray();  // return as int[][]


....

static int MyIntegerParse(string possibleInt)
{
     int i;
     return int.TryParse(possibleInt, out i) ? i : 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM