Trying to add 2 files to seperate lists to read from, I need to combine both into one list

Viewed 274

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.
2 Answers

Improvements

Use built-in function like ReadAllLines or ReadAllText to make the reading operation more safe.

If the list of names, in the test file, are arranged as follows

*.txt

Name Surmane
Name Surmane
...

Use the following code to read the content

string[] names = File.ReadAllLines(@"BoyNames.txt");

If there are a separator and all names are on the same line use

string namesString = File.ReadAllLines(@"BoyNames.txt");
string[] names = namesString .Split(',');

Procedure

The following code search for a name key culture-insensitive.

using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            string test = "kenoby";

            List<string> boyNameList = new List<string>() {"Luke Skywalker", "Han Solo", "Obi Wan Kenoby"};
            List<string> girlNameList = new List<string>() { "Leila Skywalker" };

            List<string> combinedList = new List<string>();
            combinedList.AddRange(boyNameList);
            combinedList.AddRange(girlNameList);

            int results = combinedList.Where(n => n.Contains(test, StringComparison.OrdinalIgnoreCase)).Count();
            if (results > 0)
            {
                Console.WriteLine("The name you entered is among the most popular names.");
            } 
            else
            {
                Console.WriteLine("The name you have entered is not among the most popular names.");
            }

            Console.ReadLine();
        }
    }
}

Output

The name you entered is among the most popular names.

There is no need for combinedList at all. This is just a waste of memory and CPU time making extra copies of the lists.

Below uses LINQ to search both lists:

string nameToSearch = "Some Name";
//if match is true, it exists in one of the lists
bool match = boyNameList.Any(x => x == nameToSearch) || girlNameList.Any(x => x == nameToSearch);

Other things to consider are using ReadAllLines and exactly how you want to define a match between two strings. The above code doesn't address things like white space, culture, partial match, etc..

I'd look at the documentation found here as a good start for string comparison. There's more to it than meets the eye at first glance.

Related