C# 未存储列表值
有人能告诉我我遗漏了什么吗?这个类从文本文件中读取值,并且应该存储它们以供其他使用,我可以用C# 未存储列表值,c#,list,text-files,textreader,file.readalllines,C#,List,Text Files,Textreader,File.readalllines,有人能告诉我我遗漏了什么吗?这个类从文本文件中读取值,并且应该存储它们以供其他使用,我可以用 Console.Write(eachCell[0])但我无法保存值。我试过使用 string[]和List但运气不好。它应该存储正在读取到列表或数组的值,但到目前为止还没有。控制台上没有显示任何内容 class test { public void tryOut() { #region file.read try {
Console.Write(eachCell[0])
但我无法保存值。我试过使用
string[]
和List
但运气不好。它应该存储正在读取到列表或数组的值,但到目前为止还没有。控制台上没有显示任何内容
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]@
类测试
{
公开无效试用()
{
#区域文件.read
尝试
{
字符串fileLocation=@“shelfInfo.txt”;
列表单元格=File.ReadAllLines(fileLocation.ToList();
aray(细胞);
}
抓住
{
新文件NotFoundException();
}
#端区
}
公共空白区域(列表单元格)
{
字符串[]t=新字符串[20];
字符串[]k=新字符串[20];
字符串a=“”,b=“”,c=“”,d=“”;
int i=0;
foreach(单元格中的字符串单元格)
{
字符串[]eachCell=cell.Split('@','\t','\n');
a=t[0]=eachCell[0]。ToString();//Consol.Write on eachCell直接显示正确的值。
b=t[1]=eachCell[1].ToString();
c=t[2]=eachCell[2].ToString();
d=t[3]=eachCell[3].ToString();
对于(;i
我也非常感谢您提供有关异常处理的提示。您的程序不会返回任何内容,因为您使用了以下代码
catch { new FileNotFoundException(); }
Console.Write
仅因为捕获到异常而不返回任何内容,但不会引发新异常。出现异常是因为eachCell
不包含4个元素,您尝试访问该元素。事实上,除非您想手动处理此异常,否则您不必执行try catch
。如果文件不在那里,则会自动抛出FileNotFoundException
。更改试用方法,如下所示
public void tryOut()
{
#region file.read
var fileLocation = @"Path";
aray(File.ReadAllLines(fileLocation).ToList());
#endregion
}
public static void aray(List<string> cells)
{
List<string> t = new List<string>();
foreach (string cell in cells)
{
string[] eachCell = cell.Split('@', '\t');
foreach (var e in eachCell)
{
t.Add(e);
}
}
foreach (var e in t)
{
Console.WriteLine(e);
}
}
公开作废试用()
{
#区域文件.read
var fileLocation=@“路径”;
aray(File.ReadAllLines(fileLocation.ToList());
#端区
}
公共静态空白区域(列表单元格)
{
列表t=新列表();
foreach(单元格中的字符串单元格)
{
字符串[]eachCell=cell.Split('@','\t');
foreach(每个单元格中的变量e)
{
t、 加(e);
}
}
foreach(t中的变量e)
{
控制台写入线(e);
}
}
PutConsole。在foreach
loop中写入。要在哪里“保存”值?List ne=new List()的含义是什么;int i=0代码>?你好像没用它。为什么要声明inti=0代码>是否在for
循环之外?对于循环(用a
多次填充k
),该有什么用?如果你从未使用过,为什么要填写k
?专业提示:在提出问题之前,请务必将代码整齐地格式化和缩进,不要让我们陷入混乱。。。另外,要清楚出错的地方:“我不能保存”根本不能告诉我们正在发生什么和您希望发生什么。我使用“列表ne”来保存值,但这不起作用。我将删除它'int i',而'for()'循环将遍历'ne''字符串[]k',还应存储值+1,以便从try
中提取数组(单元格)
。问题是,如果这个尝试..catch
甚至有意义。如果文件不在那里,则会抛出FileNotFoundException
。也许如果(File.Exists(fileLocation)){…}
将有助于最小化(但不是完全消除!)尝试读取不存在的文件的风险。如果不使用“try…catch”,程序将陷入无休止的写循环是的,我有。读取值工作正常,但程序还需要保存值(用于代码的其他部分)。当您使用上述代码时,它是否引发异常或显示b c d的值?我已经尝试过了。我应该抛出一个IndexOutOfRangeException,或者显示该值。它抛出IndexOutOfRangeException,因为您使用cell.Split(“@”、“t”、“n”)。只要txt
没有至少3个'@'或'\t',eachCell
就不会有4个元素<代码>\n因为单元格
已经是每行字符串的列表,所以拆分中的无效。