【问题标题】:List values are not being stored未存储列表值
【发布时间】: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&lt;string&gt; ne = new List&lt;string&gt;(); 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


【解决方案1】:

您的程序没有返回任何内容,因为您使用了以下代码。

catch { new FileNotFoundException(); }

Console.Write 不返回任何内容,因为异常被捕获但它不会引发新的异常。有一个例外,因为eachCell 不包含 4 个元素并且您尝试访问该元素。事实上,除非您想手动处理此异常,否则您不必执行 try-catch。如果该文件不存在,FileNotFoundException 将自动抛出。修改tryOut方法如下。

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);
    }
}

【讨论】:

  • +1 表示将aray(cells) 带出try。问题是,这个try..catch 是否有意义。如果文件不存在,FileNotFoundException 将被抛出。也许if (File.Exists(fileLocation)) { ... } 有助于最小化(但不能完全消除!)尝试读取不存在的文件的风险。
  • 不使用 'try...catch' 程序陷入无休止的写入循环
  • 是的,我有。读取值工作正常,但程序还需要保存值(用于代码的其他部分)。
  • 使用上面的代码时,是抛出异常还是显示a b c d的值?
  • 我试过了。我应该抛出一个 IndexOutOfRangeException 或可能显示该值。它会抛出 IndexOutOfRangeException,因为您使用了 cell.Split('@', '\t', '\n')。每当您的 txt 没有至少 3 个 '@' 或 '\t' 时,eachCell 将不会有 4 个元素。拆分中的\n 没用,因为cells 已经是每行的字符串列表。
猜你喜欢
  • 2021-05-13
  • 2012-04-10
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多