Purpose: This app. reads the girls names from the GirlNames.txt file & boy names from the BoyNames.txt file. This app puts these names into separate arrays & when the user enters a name of a boy &/or a girl, the app displays a message indicating whether the name(s) were among the most popular names from 2000-2009.
private void searchButton_Click(object sender, EventArgs e)
{
try
{
//Opens BoyNames.txt & writes to list.
StreamReader boys;
string boyNames;
boys = File.OpenText("BoyNames.txt");
List<string> boyNameList = new List<string>();
//Opens GirlNames.txt & writes to list.
StreamReader girls;
string girlNames;
girls = File.OpenText("GirlNames.txt");
List<string> girlNameList = new List<string>();
// Get user input.
boyNames = boyNameTextBox.Text;
girlNames = girlNameTextBox.Text;
//Read Boys names
while (!boys.EndOfStream)
{
boyNameList.Add(boys.ReadLine());
} // end While loop.
//Read girls names
while (!girls.EndOfStream)
{
girlNameList.Add(girls.ReadLine());
} // end While loop.
int boysPosition = boyNameList.IndexOf(boyNames);
int girlsPosition = girlNameList.IndexOf(girlNames);
if (boysPosition != -1)
{
MessageBox.Show("The boy name you entered is one of the most popular names.");
} // end boys position IF loop.
if (girlsPosition != -1)
{
MessageBox.Show("The girl name you entered is one of the most popular names.");
} // end girlsPosition IF loop
if (boysPosition != -1 && girlsPosition != -1)
{
MessageBox.Show("The name you entered was found on both the boys and girls name lists!");
}
else if (boysPosition == -1)
{
MessageBox.Show("The boy name you have entered is NOT one of the most popular names.");
} // end Else If.
else if ( girlsPosition == -1)
{
MessageBox.Show("The girl name you have entered is NOT one of the most popular names.");
} // end Else If.
} // end try block.
catch (Exception ex)
{
// Display and error message.
MessageBox.Show(ex.Message);
} // end catch block.
} // end searchButton.