How do you get out of infinite loop when there's no match?

Viewed 104

So I'm trying to test my validations of when a user types in the wrong 5 digit number then it will display an error and go back to the original form. However, my code makes the code go into an infinite loop so I'm unable to do anything else on my form since the loop never ends.

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

Here is the get record method from another class

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

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

        return nextRecord;
    }// end getNextRecord

Here is the textfile

 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 Answers
Related