繁体   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