This is the data file named "ChargeAccounts.txt". I need to search this data file for a users account number entered in the textbox txtAccount.
5658845
4520125
7895122
8777541
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
I need to accomplish this using this array.
const int SIZE = 18;
string[] acct = new string[SIZE];
This is the code I have and it will only display "Account is invalid." even when I enter a account number from the data file.
private void FindAccountNumber()
{
System.IO.StreamReader file =
new System.IO.StreamReader("ChargeAccounts.txt");
acct = File.ReadAllLines("ChargeAccounts.txt");
for (int i = 0; i < acct.Length; i++)
{
if (txtAccount.Text.Contains(acct[i]))
{
lblMessage.Text = "Account is valid";
}
else
{
lblMessage.Text = "Account is invalid.";
}
}
file.Close();
}
You are iterating over all elements and updating lblMessage.Text
for every iteration. So, you can get valid state only if txtAccount.Text
contains last number in the file. You should break the for
statement when valid number found, it will look like this
string message = "Account is invalid.";
for (int i = 0; i < acct.Length; i++)
{
if (txtAccount.Text.Contains(acct[i]))
{
message = "Account is valid";
break;
}
}
lblMessage.Text = message;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.