当没有匹配时,你如何摆脱无限循环?

时间:2016-03-07 16:27:04

标签: c# .net file while-loop

因此,我试图测试用户输入错误的5位数时的验证,然后它会显示错误并返回原始表单。但是,我的代码使代码进入无限循环,因此我无法在表单上执行任何其他操作,因为循环永远不会结束。

public bool findCustomer(string accountNumber)
{
    string record = Global.currentFile.getNextRecord();                 //gets first record
    bool okay = Global.customer.matchCustomer(accountNumber, record);   //checks if it matches
    while (!okay == true)                                                       //if it does not match, get next record and check again until it reaches end of file
    {
         record = Global.currentFile.getNextRecord();      
         okay = Global.customer.matchCustomer(accountNumber, record);
    }

        return okay;                                                                                  
}//end method

这是来自另一个类

的get记录方法
public string getNextRecord()
{
    string nextRecord = String.Empty;

    while ((nextRecord = reader.ReadLine()) != null)
    {
        return nextRecord;
    }

        return nextRecord;
    }// end getNextRecord

这是文本文件

 12345 * Shrek * 1209 * 100000 * 50000
 12077 * Sammy Wheeler * 1207 * 5000 * 0
 99999 * The Big Grump * 1298 * 1500000 * 1500000
 13579 * Brooks Robinson * 5555 * 225000  * 225000
 24680 * Johnny Unitas * 1919 * 60000 * 34000
 68420 * Y. A. Tittle * 1414 * 42000 * 12000
 23456 *  Hilary Clinton * 2222  * 65000 * 123456
 23232 * Julianne Baird * 1234  * 145000 * 12321

3 个答案:

答案 0 :(得分:2)

您应该处理getNextRecord()返回null或空字符串

的情况
while (!okay)                                                       
{
     record = Global.currentFile.getNextRecord();   
     if (string.IsNullOrWhiteSpace(record)
         break;   
     okay = Global.customer.matchCustomer(accountNumber, record);
}

请注意,如果文件中间包含空字符串,那么这将失败,并且该空行之后的行将被转义。

为什么同时检查nullstring.Empty?因为如果读者到达文件末尾,则nextRecord为空。

答案 1 :(得分:2)

在您的代码中,没有下一条记录意味着您将返回string.Empty

public string getNextRecord()
{
    string nextRecord = String.Empty;

    while ((nextRecord = reader.ReadLine()) != null)
    {
        return nextRecord;
    }

        return nextRecord;
}// end getNextRecord

您可以简单地使用此信息从您的循环中退出:

record = "initval";
while (!okay == true && !string.IsNullOrEmpty(record))                                                       //if it does not match, get next record and check again until it reaches end of file
{
     record = Global.currentFile.getNextRecord();      
     okay = Global.customer.matchCustomer(accountNumber, record);
}

尽管如此,您可以通过移除getNextRecord()并将!okay == true更改为!okay来进一步简化您的代码:

public bool findCustomer(string accountNumber)
{
    string record = reader.ReadLine(); //gets first record
    bool okay = Global.customer.matchCustomer(accountNumber, record);   //checks if it matches
    while (!okay && !string.IsNullOrEmpty(record))                                                       //if it does not match, get next record and check again until it reaches end of file
    {
         record = reader.ReadLine(); //why not this?
         if (record != null)  
             okay = Global.customer.matchCustomer(accountNumber, record);
    }

        return okay;                                                                                  
}//end method

答案 2 :(得分:1)

首先,lil建议(!okay == true)(!okay)相同,只是个人偏好。关于代码,IMO,你永远不会检查你是否已经完成阅读文件。当没有更多记录时,你只需返回“”并且你的代码一直调用Global.customer.matchCustomer(accountNumber, "");,它总是返回false,因此,无限循环。考虑while (!okay && record != String.Empty)