开发者

Error in parsing text file

Guys, I have an error with my code and i do not understand why, here's my code:

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开发者_C百科; 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


}

} // Here The Text File Sample 10192 20351 30473 40499 50449 60234 10192 20207 30206 40203 50205 60226 10192 20252 30312 40376 50334 60252 10192 20271 30332 40405 50209 60234

can someone show me what i am doing wrong?

what i'm wanting to do is, i have a text file containing a number, i want to parse it, and put each number in an array so i can access it easily. Is my code effective enough?

thanks, alot for a suggestion.


Your main mistake seems to be this:

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

this only instantiates an array of arrays, not the nested arrays itself, so when you write

number[i][j] = ...

number[i] returns null, and calling indexer on a null is a NullReferenceException!

Edit:
What you should write is:

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


In my opinion, this is easier without the array initialization (skip this answer if you're doing this as an exercise to using arrays in 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜