c#程序输入字符串的格式不正确

private void button1_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
StreamReader sr = new StreamReader(path);
string[] s = sr.ReadToEnd().Split('\n');
for (int i = 0; i < DATA; i++)
{
string[] a = s[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < 6; j++)
{
data_in[i, j] = Convert.ToDouble(a[j].ToString());
}

}
}

第1个回答  2019-09-06
private void button1_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
StreamReader sr = new StreamReader(path);
string[] s = sr.ReadToEnd().Split('\n');
for (int i = 0; i < DATA; i++)
{
string[] a = s[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

//如果确定可以不添加
if (a.Length == 6)
{
double temp = 0;

for (int j = 0; j < 6; j++)

{

if (double.TryParse(a[j].ToString(), out temp))
{
data_in[i, j] = Convert.ToDouble(a[j].ToString());
}

}
}

}
}
}追问

这个temp没有声明呢

第2个回答  2019-09-05
就是你输入的字符串不是数字,以一定要转换为double 就 抛出异常了
我估计 那个Text的值是空串追问

那怎么办呢

本回答被网友采纳
第3个回答  2019-09-05
如果文本格式比较不规则,最好用正则表达式拆分
可以帮写,私信
相似回答