【发布时间】:2017-05-18 10:10:25
【问题描述】:
谁能告诉我我缺少什么,该类从文本文件中读取值并应将它们存储以供其他用途,我可以看到这些值
Console.Write(eachCell[0]) 但我无法保存这些值。我试过使用
string[] 和 List<string> 但没有运气。它应该将读取的值存储到列表或数组中,但到目前为止还不是。控制台上没有显示任何内容。
class test
{
public void tryOut()
{
#region file.read
try
{
string fileLocation = @"shelfInfo.txt";
List<string> cells = File.ReadAllLines(fileLocation).ToList();
aray(cells);
}
catch
{
new FileNotFoundException();
}
#endregion
}
public void aray(List<string> cells)
{
string[] t = new string[20];
string[] k = new string[20];
string a = "", b = "", c = "", d = "";
int i = 0;
foreach (string cell in cells)
{
string[] eachCell = cell.Split('@', '\t', '\n');
a = t[0] = eachCell[0].ToString(); //Consol.Write on eachCell directly shows the right values.
b = t[1] = eachCell[1].ToString();
c = t[2] = eachCell[2].ToString();
d = t[3] = eachCell[3].ToString();
for (; i < eachCell.Length; i++)
{
k[i] = a; //should store the values from eachCell
}
}
Console.Write(" " + a + " " + b + " " + " " + c + " " + d); //means the value are being receivedbut no stored
}
}
// contents of text file
//A11[1]@ A12[0]@ A13[1]@ A14[1]@
//A21[1]@ A21[1]@ A23[0]@ A24[0]@
//A31[1]@ A32[0]@ A33[1]@ A34[1]@
//A41[1]@ A41[1]@ A43[0]@ A44[0]@
我也非常感谢有关异常处理的任何提示。
【问题讨论】:
-
将
Console.Write放入foreach循环中。 -
你想在哪里“保存”值?
-
List<string> ne = new List<string>(); int i = 0;是什么意思?你似乎没有使用它。为什么在for循环之外声明int i = 0;?for循环有什么用(用a填充k很多次)?k你从来不用,为什么要填? -
专业提示:在将代码放入问题之前,请务必整齐地格式化和缩进代码,不要让我们陷入混乱......另外,要清楚哪里出了问题:“我可以” t save”并没有告诉我们正在发生的事情与您希望发生的事情。
-
我正在使用 'List
ne' 来保存值,但这不起作用。我将删除它'int i'并且'for()'循环是迭代'ne''string [] k'也应该存储值
标签: c# list text-files textreader file.readalllines