What would be a simple coding solution to the following problem? I have two file
ID: 3856649 • Letter: W
Question
What would be a simple coding solution to the following problem?
I have two files "GirlNames.txt" and "BoyNames.txt" - Both files contain a list of the most popular names given to girls and boys born in the US from 2000 to 2009. I cannot figure out how to code an application that reads the contents of the two files into two separate arrays or Lists and have the user be able to enter a name and have the application display messages indicating whether the name was found in either of the lists of most popular names.
Textbook Question - "Starting Out With Visual C# (4th edition)" - Chapter 7 Programming Problem #6 (6PP)
Thank you for any help you can give.
Explanation / Answer
here is the code:
class BabyName
{
static void Main(string[] args)
{
try
{
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("boynames.txt");
string myString = myFile.ReadToEnd();
//converting boy names into array
//every name in the txt file must be end with comma (,)
//all names we are storing at boyArrays
String[] listLines = myString.Split(',');
List<List<int>> boyArrays = new List<List<int>>();
for (int i = 0; i < listLines.Length; i++)
{
List<int> array = new List<int>();
String[] listInts = listLines[i].Split(' ');
for(int j=0;j<listInts.Length;j++)
{
if (listInts[j] != " ")
{
array.Add(Convert.ToInt32(listInts[j]));
}
}
boyArrays.Add(array);
}
//converting girl names into array
//every name in the txt file must be end with comma (,)
//all names we are storing at girlArrays
String[] listLines = myString.Split(',');
List<List<int>> girlArrays = new List<List<int>>();
for (int i = 0; i < listLines.Length; i++)
{
List<int> array = new List<int>();
String[] listInts = listLines[i].Split(' ');
for(int j=0;j<listInts.Length;j++)
{
if (listInts[j] != " ")
{
array.Add(Convert.ToInt32(listInts[j]));
}
}
girlArrays.Add(array);
}
Console.WriteLine("Please Enter Name to Search....");
Console.WriteLine("----");
String s;
s = Console.ReadLine();
for(int i=0;i<s.length;i++)
{
if(s==boyArrays[i]){
console.WriteLine("Name Found in boys List");
break;
}
else{
if(s==girlArrays[i]){
console.WriteLine("Name Found in Girls List");
break;
}
else{
console.WriteLine("Name Not Found") ;
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.