Reputation: 91
I'm trying to make a program that reads a text file and returns data from it (in this case, currencies). The text file has all currencies listed in this fashion:
USD US Dollars (USA) 1,077600 1,058100 1,097100
JPY Yen (Japan) 133,080000 130,480000 135,680000
... etc.
So when a user inputs a currency code (let's say JPY), the program would print:
JPY Yen (Japan) 133,080000 130,480000 135,680000
And after that, the program would loop and keep asking currencies until the user inputs an empty string.
This is what I have now:
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
public class Currencies
{
public static void Main()
{
string line;
System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt");
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
while ((line = currencies.ReadLine()) != null)
{
if (line.Contains(currency))
{
Console.WriteLine(line);
}
}
Console.ReadLine();
}
}
Now, it returns the correct line, but the loop breaks after the first input, and if you input an empty string, it returns every line of the text file.
Any ideas how to continue making this? Also, should I use ReadAllLines instead of StreamReader?
Upvotes: 2
Views: 1570
Reputation: 10078
You can use the following code. Explanations inline
public static void Main()
{
//read all lines from file only once, and keep for future use
var currencyDetails = File.ReadAllLines(@"C:\YourDirectory\currencies.txt");
string input = string.Empty;
while (true) //keep looping until empty input by user
{
Console.Write("Enter currency code > ");
input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) //stop loop
break;
//from all lines of file, get the line where line starts with the user input e.g. usd
var currencyDetail = currencyDetails.FirstOrDefault(l => l.StartsWith(input.ToUpper()));
if (currencyDetail != null) //if a matching currency is found, show it
{
Console.WriteLine(currencyDetail);
}
}
Console.ReadLine();
}
Upvotes: 1
Reputation: 355
this should solve it in the most effective way:
public static void Main()
{
string line;
bool IsEmptyString = false;
List<string> lines = new List<string>();
using (System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt")
{
while ((line = currencies.ReadLine()) != null)
{
lines.Add(line);
}
}
while (!IsEmptyString)
{
string tempLine = "";
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
IsEmptyString = currency == "" ? true : false;
tempLine = lines.FirstOrDefault(x => x.Contains(currency));
if (tempLine!="")
{
Console.WriteLine(tempLine);
}
tempLine = "";
}
}
Upvotes: 2
Reputation: 12196
ReadAllLines vs StreamReader
If you text file is really big, than scanning the file row by row is a good practice. In any other case to ReadAllLines
will be a better choice, especially when scanning several times.
Empty string returns results
You're using the Contains
method, which in turns search inside the string an empty string. Every string has inside of her an Empty string.
Solution: Test to see whatever the user entered an empty string and handle it the way you see fit.
Input only once
You're only searching for the input once, than it can't guess you want it to rescan again.
Solution: Make another loop, which will wrap the current loop until you need it to stop.
Upvotes: 2