please i need help with this problem Pg. 453 #17 Name Search If you have downloa
ID: 3798532 • Letter: P
Question
please i need help with this problem
Pg. 453 #17
Name Search
If you have downloaded this book’s source code from the companion Web site, you will find the following files in this chapter’s folder:
GirlNames.txt—This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2009.
BoyNames.txt—This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2009.
Write a program that reads the contents of the two files into two separate arrays or vectors. The user should be able to enter a boy’s name, a girl’s name, or both, and the application should display messages indicating whether the names were among the most popular.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Names
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Opens BoyNames.txt
StreamReader inputFile = File.OpenText("BoyNames.txt");
List<string> nameList = new List<string>();
//Opens GirlNames.txt
StreamReader inputFile2 = File.OpenText("GirlNames.txt");
List<string> namelist2 = new List<string>();
List<string> combinedList = new List<string>();
combinedList.AddRange(nameList);
combinedList.AddRange(namelist2);
private void buttonsubmit_Click(object sender, EventArgs e)
{
//Read Boys names
while (!inputFile.EndOfStream)
{
nameList.Add(inputFile.ReadLine());
}
//Read girls names
while (!inputFile2.EndOfStream)
{
namelist2.Add(inputFile2.ReadLine());
}
//Search for name entered by user in boys list.
int position = nameList.IndexOf(textBoxName.Text);
if (position != -1)
{
MessageBox.Show("The name you entered is among the most popular names.");
}
else
{
MessageBox.Show("The name you have entered is not among the most popular names.");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.