2016-03-07 37 views
3

所以我试图测试我的验证当用户键入错误的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 

这里是从另一个类

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 
+1

我强烈建议您使用数据库系统。如果您需要将记录存储在文件中,请转至SQLite。你为什么试图重新发明轮子? –

回答

2

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

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

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

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

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 
+0

你应该检查'record'的空值和空值。 – dotctor

+0

@dotctor你是对的,更新 – Ian

+0

'!okay == true'是完全错误的。'!okay'会照顾你的问题。好的,也许不是全部,但是接受几张海报指出是非常重要的。 – hylander0

1

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