【发布时间】:2011-10-06 00:04:56
【问题描述】:
运行此程序时出现错误。代码所做的是从文本文件中读取行,句子用逗号分隔,我所做的是拆分它们并将它们放入多维数组中,但在运行时出现错误:
"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"
代码:
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");
}
}
【问题讨论】:
-
你得到什么例外? 使用调试器!
-
@SLaks:这是他对use unassigned local variable 'multidimension' 的跟进。继续调试四个问题......到目前为止。
-
您已就同一问题发布了三个问题。这个答案:stackoverflow.com/questions/7669026/… 包含您的问题的解决方案。主要问题是当您不知道有多少行时,您正在尝试构建一个多维数组。他建议使用
List<string[]>是一个很好的建议。
标签: c# multidimensional-array split