我想创建代码,将几个文本文件(每个文件具有不同的结构)连接成一个具有统一构建的文本。
现在,我设法打开所有文件,按制表符分割行中的字符串,并将所有内容保存到一个二维列表中。
Example of current data:
809187.49 226885.80 26934
809183.14 226877.21 26937a
2 5509514.58 6558911.86 0.00 80T
3 5509515.55 6558913.48 0.00 80T
4 5509516.35 6558914.56 0.00 80T
接下来,我将从二维列表中重新打印内容到一个新数组(包含n行和2列)并同时对所有数据进行排序。所有双重类型,大于100000.00我想用','进入第二列。分隔符和列表中每行的其余内容我想放入第一列:
Result:
26934 809187.49,226885.80
26937a 809183.14,226877.21
2,0.00,80T 5509514.58,6558911.86
3,0.00,80T 5509515.55,6558913.48
4,0.00,80T 5509516.35,6558914.56
这就是问题所在。我使用了如下代码:
string[,] resultArray = new string[RowNo, 2]; //Create Array
for (int i = 0; i < resultList.Count; i++)
{
for (int j = 0; j < Regex.Matches(file[i], " ").Count+1; j++)
{
if (double.TryParse(resultList[i][j], out n)) // record is a double type greater then n
{
if((Double.Parse(resultList[i][j]) % 1) > 0) // record is not a int type
{
if (resultArray[i, 1] != null) // if cell in array is not null
{
resultArray[i, 1].Insert(resultArray[i, 1].Count(), "," + resultList[i][j].ToString()); // add content of List in to string in second column
}
else // if cell in array is null for now
{
resultArray[i, 1].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to second column
}
}
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
}
else
{
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
}
}
}
但不幸的是,当我运行代码时,我收到错误&#34;对象引用没有设置为对象的实例&#34;
我做了一些研究并尝试这样的事情:
resultArray[i, 1] += resultArray[i, 1] + "," + res_file[i][j].ToString();
代替
resultArray[i, 1].Insert(resultArray[i, 1].Count(), "," + res_file[i][j].ToString());
但它没有正常工作。我收到了一些结果,但记录中的数据是重复的。
我将感激你的每一个帮助。
答案 0 :(得分:1)
问题在于你所有其他条件:
你在做什么
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
这基本上意味着在resultArray[i, 0] != null
时阻塞,但在else块中你试图访问该null对象的Insert()
方法。因此错误。您的所有else
块都是这样的。
我认为你想要做的是在你的其他区块(你似乎已经想到了)中的一项任务......就像:
resultArray[i, 0] += "," + resultList[i][j].ToString());
重复的问题可能是您正在使用+=
和左侧(LHS)表达式
resultArray[i, 1] += resultArray[i, 1] + "," + res_file[i][j].ToString();
当您执行x += 1;
时,它与x = x + 1;
您正在执行:x += x + 1;
与x = x + (x + 1);