开发者

read file multidimensional array c# split [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

use unassigned local variable 'multidimension'

I am getting an error when I run this program. what the code do is read from text file rows and sentences is separated from each other by a comma and what I did is split them and put them in multi-dimensional array but I am getting an error at run time which is:

"unhanded exception has occurred in your application if you click continue the application will ignore this error and attempt to continue if you click quit the application will close immediately"

Code:

try {
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK) {

        using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) {  
            string[] data= null;
          开发者_如何学编程  string ReadFromReadLine;
            ReadFromReadLine = sr.ReadLine();
            string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];

            while (ReadFromReadLine != null) {
                data = ReadFromReadLine.Split(',');

                for (int i = 0; i <= ReadFromReadLine.Length; i++) {
                    for (int j = 0; j <= data.Length; j++ ) {
                        multidimensional[i, j] = data[j];
                    }
                }    
            }

            for(int i = 0 ; i<ReadFromReadLine.Length;i++) {
                for(int j = 1; j<= data.Length ; j++) {
                    textBox1.Text +=  multidimensional[i,j];
                }
            }
        }
        FilePath.Text = openFileDialog1.FileName;
        //textBox1.Text += (string)File.ReadAllText(FilePath.Text);
    }
}
catch(IOException ex) {
    MessageBox.Show("there is an error" + ex+ "in the file please try again");
}
}


The error is here:

ReadFromReadLine = sr.ReadLine();
string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];

When end of file is encountered, ReadLine() returns null. Then the second line crashes because ReadFromReadLine.Length cannot be computed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜