I'm sure I'm making this much harder than it really is, but my brain hurts....
I have a program that reads a list of baseball teams from a .txt file (called Teams) and displays them into a listbox. I have another .txt file (called WorldSeries) that displays, in order, which teams have won the World Series. The WorldSeries file looks like this (minus the bullet points):
- Boston Americans
- New York Giants
- Chicago White Sox
- Chicago Cubs
- Chicago Cubs
- Pittsburgh Pirates
- ...
Meaning The Boston Americans won the first World Series, Giants second, White Sox third, etc. The dates range from 1903-2009 accordingly, however there was no World Series in 1904 or 1994.
The program allows you to select a team from the list box, click a button, and a MessageBox tells you how many times that team has won the World Series. I thought it sounded pretty easy, but I'm having a harder time than I thought I would. I think I'm doing it way wrong. Can someone please help. Thanks!
private int numberWins(int[] iArray) // Method to count total number of wins
{
int totalWins = 0; // Accumulator, intitialized to 0.
// Step through the array, adding each element to the Accumulator
for (int index = 0; index < iArray.Length; index++)
{
totalWins += iArray[index];
}
// Return the number of wins
return numberWins;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void worldSeriesApp_Load(object sender, EventArgs e)
{
try
{
string teamName; // To hold the teams name
StreamReader inputTeams; // For file input
inputTeams = File.OpenText("Teams.txt"); // Open the file for team list
teamList.Items.Clear(); // Clear all items currently in the List Box
while (!inputTeams.EndOfStream) // While not at the end of the list of teams
{
teamName = inputTeams.ReadLine(); // Read each line
teamList.Items.Add(teamName); // Add each line to the List Box
}
inputTeams.Close(); // Close the file
}
catch (Exception ex) // Catch any errors
{
MessageBox.Show(ex.Message); // Let the user know there is an error
}
}
private void button1_Click(object sender, EventArgs e)
{
// Local variables
const int SEASONS = 104; // Number of Seasons between 1903 and 2009
int[] championships = new int [SEASONS]; // Array of seasons
StreamReader champFile; // For file input
int index = 0; // Loop counter
string selectedTeam; // Team selected from the List Box
// Open the file and get StreamReader object
champFile = File.OpenText("WorldSeries.txt");
//Read the files contents into the array
while (index < championships.Length && !champFile.EndOfStream)
{
championships[index] = int.Parse(champFile.ReadLine());
index ++;
}
if (teamList.SelectedIndex !=-1) // If a team is selected
{
//Get the selected item.
selectedTeam = teamList.SelectedItem.ToString();
// Determine if the team is in the array.
if (numberWins(champFile, selectedTeam) != 1) // If so...
{
// Display how many times that team has won the World Series
MessageBox.Show("The " + selectedTeam + "has won the World Series"
numberWins + "times!");
}
else // Otherwise..
{
// Let them know their team sucks.
MessageBox.Show("The " + selectedTeam + "sucks because they have never
won the World Series!");
}