Domanda

It's suppose to allow the user to choose a team from the ListBox, click a button and display the number of times that team has won the World Series by referring to a .txt file (called World Series).

I'm trying to take the selected item and check how many times that selected item appears in the World Series .txt file.

Any pointers?

    private void compareList()
    {
        StreamReader champFile = File.OpenText("WorldSeries.txt");
        List<string> champList = new List<string>();
        string selectedTeam;

        while (!champFile.EndOfStream)
        {
            champList.Add(champFile.ReadLine());
        }
        while (teamList.SelectedIndex != 0)
        {
            selectedTeam = teamList.SelectedItem.ToString();
        }
        if (selectedTeam = champList???)
        {
            MessageBox.Show("The " + selectedTeam + "has won the World Series " + champList.????.Count + "times! ")
        }
        else
        {
            MessageBox.Show("The " + selectedTeam + "has never won the World Series.")
        }
    } 
È stato utile?

Soluzione

You can use File.ReadLines and some LINQ

selectedTeam = teamList.SelectedItem.ToString();
var count = File.ReadLines("WorldSeries.txt")
           .Count(x => x.Contains(selectedTeam));


if(count > 0)
{
   MessageBox.Show("The " + selectedTeam + "has won the World Series " +  count + "times! ")
}

Altri suggerimenti

if (teamList.SelectedIndex == -1)
{
   MessageBox.Show("Please select an Item first!");
   return;
}

string selectedTeam = teamList.SelectedItem.ToString();
var count = File.ReadAllLines("WorldSeries.txt").Where(line => line ==selectedTeam).Count();

//var count = champList.Where(l=>l==selectedTeam).Count();

if (count >0)
{
    MessageBox.Show("The " + selectedTeam + "has won the World Series " + count  + "times! ");
}
else
{
    MessageBox.Show("The " + selectedTeam + "has never won the World Series.")
}

You can do it simply this way

Updated Code

if(teamList.SelectedIndex != 0 && champList.Contains(SelectedTeam))
{
MessageBox.Show("The " + selectedTeam + "has won the World Series " + champList.Where(w => w == SelectedItem).Count() + "times! ");
}

Place the champList in a dictionary and increment a counter if they already exist.

private void compareList()
    {
        StreamReader champFile = File.OpenText("WorldSeries.txt");
        Dictionary<string, int> champList = new Dictionary<string, int>();
        string selectedTeam;
        string currentChamp;
        while (!champFile.EndOfStream)
        {
            currentChamp = champFile.ReadLine();
            if(champList.containsKey(currentChamp))
                champList.get(currentChamp) += 1;
            else
                champList.Add(champFile.ReadLine(), 1);
        }
        while (teamList.SelectedIndex != 0)
        {
            selectedTeam = teamList.SelectedItem.ToString();
        }
        if (champList.hasKey(selectedTeam))
        {
            MessageBox.Show("The " + selectedTeam + "has won the World Series " + champList.get(selectedTeam) + "times! ")
        }
        else
        {
            MessageBox.Show("The " + selectedTeam + "has never won the World Series.")
        }
    } 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top