Here is my C# code. The C# problem states: Name data text files were provided fo
ID: 3681787 • Letter: H
Question
Here is my C# code. The C# problem states:
Name data text files were provided for the problem. Create an application that reads the content of the two files into two separate array's or Lists. 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.
Here is my C# code, but it doens't search either one of my .text files boy or girl.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NameSearchApplication
{
public partial class Form1 : Form
{
//public object combinedlist { get; private set; }
public Form1()
{
InitializeComponent();
}
//Opens BoyNames.txt
StreamReader inputFile = File.OpenText(@"\psfHomeDocumentsVisual Studio 2015ProjectsNameSearchApplicationNameSearchApplicationinDebugoynames.txt");
List<string> namelist = new List<string>();
//Opens GirlNames.txt
StreamReader inputFile2 = File.OpenText(@"\psfHomeDocumentsVisual Studio 2015ProjectsNameSearchApplicationNameSearchApplicationinDebuggirlnames.txt");
List<string> namelist2 = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
List<string> combinedList = new List<string>();
combinedList.AddRange(namelist);
combinedList.AddRange(namelist2);
}
private void button1_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(textBox1.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.");
}
}
}
}
Explanation / Answer
The entire code is fine, except that the sequence of code is wrong:
You are combining empty list, that is wrong, get data into namelists and then combine them. Also, you are searhing in namelist which is wrong, search in combinedlist.
Modified Code:
private void button1_Click(object sender, EventArgs e)
{
//Opens BoyNames.txt
StreamReader inputFile = File.OpenText(@"\psfHomeDocumentsVisual Studio 2015ProjectsNameSearchApplicationNameSearchApplicationinDebugoynames.txt");
List<string> namelist = new List<string>();
//Opens GirlNames.txt
StreamReader inputFile2 = File.OpenText(@"\psfHomeDocumentsVisual Studio 2015ProjectsNameSearchApplicationNameSearchApplicationinDebuggirlnames.txt");
List<string> namelist2 = new List<string>();
//Read Boys names
while (!inputFile.EndOfStream)
{
namelist.Add(inputFile.ReadLine());
}
//Read girls names
while (!inputFile2.EndOfStream)
{
namelist2.Add(inputFile2.ReadLine());
}
List<string> combinedList = new List<string>();
combinedList.AddRange(namelist);
combinedList.AddRange(namelist2);
//Search for name entered by user in boys list.
int position = combinedList.IndexOf(textBox1.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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.