【问题标题】:Compare list data to readline input将列表数据与 readline 输入进行比较
【发布时间】:2018-10-07 17:32:10
【问题描述】:

我正在将一个 txt 文件读取到一个数组中,然后从数组中获取数据并将其放入自定义类型列表中。

var StylistFile = File.ReadLines("stylist.txt").ToArray();
var lineCount = File.ReadLines("stylist.txt").Count();

int k = (lineCount) / 6; // used tocalculates number of stylists by looking at the number of lines
int L = 1;

List <Stylist> stylists = new List<Stylist>();


for (L = 1;L <=k; L++) 
{

    string[] stylistsL = new string[6];
    Stylist stylistL = new Stylist();
    stylists.Add(stylistL);

    foreach (var i in stylists) 
    {
        stylistL.FirstName = StylistFile[((L - 1) * 6) + 0];
        stylistL.LastName = StylistFile[((L - 1) * 6) + 1];
        stylistL.Email = StylistFile[((L - 1) * 6) + 2];
        stylistL.Phone = StylistFile[((L - 1) * 6) + 3];
        stylistL.Rate = StylistFile[((L - 1) * 6) + 4];

        stylistsL[0] = stylistL.FirstName;
        stylistsL[1] = stylistL.LastName;
        stylistsL[2] = stylistL.Email;
        stylistsL[3] = stylistL.Phone;
        stylistsL[4] = stylistL.Rate;
        stylistsL[5] = "";
    }
}
foreach (var i in stylists) // prints the stylists first and last names
{
    Console.WriteLine(i.FirstName + " " + i.LastName);
}

string stylistSelected = Console.ReadLine();

//foreach (var z in stylists) // goes through each stylist in list
for (int z = 0; z <= stylists.Count; z++)
{

    if ((stylistSelected == stylists[z].FirstName) || (stylistSelected == stylists[z].FirstName + " " + stylists[z].LastName)) // checks to see if stylistSelected match current stylists
    {
       Console.WriteLine(stylists[z].FirstName + " " + stylists[z].LastName);

自定义类代码是

public class Stylist 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Rate { get; set; }
}

txt 文件格式如下

john
smith
js@123.456
123456789
123

jane
doe
jd@123.456
987654321
1456

我能够获取代码以打印列表中的每个项目,但我找不到一种方法来获取代码以根据 readline 字符串检查列表中的数据我该怎么做?

我尝试过同时使用foreachfor 循环。我尝试为 forforeach 循环使用新的 int 变量

第二个问题是,我可以让stylistL,变成stylist1stylist2,......等等,以便它与txtx文件中的数据一起缩放吗?

【问题讨论】:

  • 您不需要ReadLines 两次。 lineCount = StylistFile.Length;
  • string[] stylistsL = new string[6]; 的意义何在?您在每次迭代时创建一个,分配一些值,然后在 foreach 循环结束时将其丢弃。
  • @RufusL 我认为由于 txt 文件有 5 条数据,并且每个造型师都有一个空格,我需要创建一个大小为 6 的数组,以便我更容易回调用于稍后在代码中的数据
  • “检查数据”究竟是什么意思? stylist1stylist2、...是什么意思?
  • 如果您打算稍后引用六个strings 的数组,为什么每次都通过浪费循环覆盖它的成员?为什么你需要它和Stylist 类型的对象呢?

标签: c# list for-loop if-statement foreach


【解决方案1】:

要从您的文本文件中填充List&lt;Stylist&gt;,我们可以执行以下操作,将文件读入一个数组(忽略任何空行),然后设置一个循环计数器,每次递增5迭代(因为每 5 行代表一个造型师),在循环中我们设置一个新的Stylist 的属性并将其添加到我们的List&lt;Stylist&gt;

下面的代码假设每 5 行代表一个造型师(并且它忽略文件中的任何空白行):

var fileLines = File.ReadAllLines(stylistFilePath)
    .Where(line => !string.IsNullOrEmpty(line))
    .ToList();

var stylists = new List<Stylist>();

// We have a condition to exit the loop if 'i + 4 < fileLines.Count'
// because we attempt to access item [i + 4] inside the loop.
for (int i = 0; i + 4 < fileLines.Count; i += 5)
{
    stylists.Add(new Stylist
    {
        FirstName = fileLines[i],
        LastName = fileLines[i + 1],
        Email = fileLines[i + 2],
        Phone = fileLines[i + 3],
        Rate = fileLines[i + 4]
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多