Reputation: 1
Doing some console C# homework where I'm trying to get the user to search for an 'account number' from a text file. If the number is found in the text file, it displays the number along with the other 6 lines ahead of it.
So when user types in '227586' it should display the rest of details from the text file on console from account number to email.
So far my code seems logical to me but I'm a bit lost and I keep getting 'account not found' despite typing account number correctly.
This what I've tried so far:
static void SearchAccount()
{
string userInput;
int counter = 0;
Console.Clear();
BorderTable();
Console.SetCursorPosition(10, 9);
Console.WriteLine("Account Number:");
Console.SetCursorPosition(25, 9);
userInput = Console.ReadLine();
System.IO.StreamReader file = new System.IO.StreamReader("12.txt");
while ((userInput = file.ReadLine()) != null)
{
if (("12.txt").Contains(userInput))
{
Console.WriteLine("Account details are found");
Console.WriteLine(userInput);
}
else
{
Console.SetCursorPosition(10, 19);
Console.WriteLine("Account not found!");
Console.SetCursorPosition(10, 20);
Console.WriteLine("Check another account? y/n");
Console.SetCursorPosition(10, 21);
string userCheck = Console.ReadLine();
if (userCheck == "y")
{
Console.Clear();
SearchAccount();
}
else if (userCheck == "n")
{
Console.Clear();
mainMenu();
}
}
counter++;
}
file.Close();
}
Here's my text file: It should display the first line after its found, then the other 6 lines ahead.
Account Number: 227586
Balance: $0
First Name: Mitchell
Last Name: Pog
Address: 123 Avenue St
Phone: 98979551
Email: [email protected]
Upvotes: 0
Views: 111
Reputation: 28948
You are searching in a string instead of a line of the file:
("12.txt").Contains(userInput)
But it must be:
lineOfFile.Contains(userInput)
At the same time you are overwriting the user's input:
userInput = file.ReadLine()
But it must be:
lineOfFile = file.ReadLine()
This is why you never get a match.
It's easier to read all lines into an array (or store the previously read lines into an array). This way you can directly get the last six lines before the match using the index on the lines array.
Also remove the recursion an use a loop.
static void SearchAccount()
{
string[] allLinesFromFile = File.ReadAllLines("12.txt");
string userCheck = "n";
while (userCheck != "n")
{
bool isLineFound = false;
Console.SetCursorPosition(10, 9);
Console.WriteLine("Account Number:");
Console.SetCursorPosition(25, 9);
string userInput = Console.ReadLine();
for (var lineIndex = 0; lineIndex < allLinesFromFile.Length; lineIndex++)
{
string currentLine = allLinesFromFile[lineIndex];
if (currentLine.Contains(userInput))
{
Console.WriteLine("Account details are found");
Console.WriteLine(userInput);
// TODO: Use another for-loop to return the previous six lines
// using the lineIndex and the allLinesFromFile array
Console.SetCursorPosition(10, 20);
Console.WriteLine("Check another account? y/n");
Console.SetCursorPosition(10, 21);
userCheck = Console.ReadLine();
isLineFound = true;
break;
}
}
if (!isLineFound)
{
Console.SetCursorPosition(10, 19);
Console.WriteLine("Account not found!");
Console.SetCursorPosition(10, 20);
Console.WriteLine("Check another account? y/n");
Console.SetCursorPosition(10, 21);
userCheck = Console.ReadLine();
}
}
Console.Clear();
mainMenu();
}
Upvotes: 1