C#输入字符串的格式不正确,急求解决
if(this.openFileDialog1.ShowDialog()==DialogResult.OK){string[]lines=File.ReadAllLine...
if (this.openFileDialog1.ShowDialog() == DialogResult.OK) {
string[] lines = File.ReadAllLines(openFileDialog1.FileName, Encoding.Default);
System.IO.StreamReader readfile = new System.IO.StreamReader(openFileDialog1.FileName);
// int line_total = 0;//总行数
/* while (readfile.ReadLine() != null) {
line_total++;
}
textBox1.Text = line_total.ToString();*/
readfile.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); // string line = readfile.ReadLine();
List<string[]> list = new List<string[]>();
while (!readfile.EndOfStream) {
string[] line = readfile.ReadLine().Split(' '); string[] arr = new string[line.Length];
for (int i = 0; i < line.Length; i++)
{
arr[i] = Convert.ToString(line[i]);
} list.Add(arr);
}
string[][] shuzu = list.ToArray();
int x;
x = int.Parse(textBox2.Text);
double[,] b = new double[39, x]; for (int i = 0; i < 39; i++)
{
for (int j = 0; j < x; j++)
{
b[i, j] = Convert.ToDouble(shuzu[i][j]);(输入字符串格式不正确)
}
}
//textBox2.Text = shuzu[3][3];
//textBox2.Text = b[2, 0].ToString();
//定义A矩阵
Matrix A2 = new Matrix(39, x);
int m = A2.getM;
int n = A2.getN;
double[,] a2 = A2.Detail;
for (int i = 0; i < 39; i++)
{
for (int j = 0; j < x; j++)
{
a2[i, j] = b[i, j];
}
}
//求A的转置 Matrix A2T = MatrixOperator.MatrixTrans(A2);
MessageBox.Show(MatrixOperator.MatrixPrint(A2T));
}
else
return; 展开
string[] lines = File.ReadAllLines(openFileDialog1.FileName, Encoding.Default);
System.IO.StreamReader readfile = new System.IO.StreamReader(openFileDialog1.FileName);
// int line_total = 0;//总行数
/* while (readfile.ReadLine() != null) {
line_total++;
}
textBox1.Text = line_total.ToString();*/
readfile.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); // string line = readfile.ReadLine();
List<string[]> list = new List<string[]>();
while (!readfile.EndOfStream) {
string[] line = readfile.ReadLine().Split(' '); string[] arr = new string[line.Length];
for (int i = 0; i < line.Length; i++)
{
arr[i] = Convert.ToString(line[i]);
} list.Add(arr);
}
string[][] shuzu = list.ToArray();
int x;
x = int.Parse(textBox2.Text);
double[,] b = new double[39, x]; for (int i = 0; i < 39; i++)
{
for (int j = 0; j < x; j++)
{
b[i, j] = Convert.ToDouble(shuzu[i][j]);(输入字符串格式不正确)
}
}
//textBox2.Text = shuzu[3][3];
//textBox2.Text = b[2, 0].ToString();
//定义A矩阵
Matrix A2 = new Matrix(39, x);
int m = A2.getM;
int n = A2.getN;
double[,] a2 = A2.Detail;
for (int i = 0; i < 39; i++)
{
for (int j = 0; j < x; j++)
{
a2[i, j] = b[i, j];
}
}
//求A的转置 Matrix A2T = MatrixOperator.MatrixTrans(A2);
MessageBox.Show(MatrixOperator.MatrixPrint(A2T));
}
else
return; 展开
3个回答
展开全部
debug一下友帆shuzu[i][j]的值是多少,有可能是本身悄告裤取出来启简就不对,不能转换成Double。出问题,自己先debug。 string s = shuzu[i][j];看s的值是多少。
更多追问追答
追问
有一个txt数据能读得出来,另一个就报错~~单个读出的shuzu[i][j]都能对应上的啊
追答
b[i, j] = Convert.ToDouble(shuzu[i][j]);(输入字符串格式不正确)
你没明白我的问题,你不是这句出错的么?这个是强制转换的,转换出错,就说明shuzu[i][j]这个的格式可能不能转换成double,你要调试一下是不是这个你取出来就格式就是不能转换成double的。设置断点:string s = shuzu[i][j];看s的值是多少,然后再 b[i, j] = Convert.ToDouble(s);
来自:求助得到的回答
展开全部
if(string.IsNullOrEmpty())
{
b[i, j] = Convert.ToDouble(shuzu[i][j]);
}
else
{
b[i, j] = 0.0;
}
{
b[i, j] = Convert.ToDouble(shuzu[i][j]);
}
else
{
b[i, j] = 0.0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是将字符串转换成数字吧,看看字符串对不对,非数字以外的字符是没话转换成数字形式的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询