尝试将2个文件添加到要读取的单独列表中,我需要将两个文件合并为一个列表

LedouxLives24

目的:这个程序。从GirlNames.txt文件读取女孩名称,从BoyNames.txt文件读取男孩名称。此应用程序将这些名称放入单独的数组中,并且当用户输入男孩和/或女孩的名称时,该应用程序会显示一条消息,指出该名称是否是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.

完全没有必要combinedList制作列表的额外副本只是浪费内存和CPU时间。

下面使用LINQ搜索两个列表:

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);

其他要考虑的事项是使用ReadAllLines以及确切地如何定义两个字符串之间的匹配。上面的代码没有解决空白,文化,部分匹配等问题。

我将在此处找到的文档作为字符串比较的良好起点。乍一看,它所具有的不仅仅是眼神。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将两个列表合并为一个

将两个列表添加到一个列表

使用AWK将两个txt文件合并为一个,并添加缺少的项目

将一个列表中的两个子列表合并为一个子列表python

将python中的两个列表合并为一个

将两个不同类型的列表添加到另一个列表中

将两个文件中的一个列合并为一个三文件

将两个不同文件中的JSON值添加到一个文件中

将两个字典值合并为一个,然后将其添加到Python中的另一个字典中

将两个列表合并为一个列表

将两个排序列表合并为一个更大的排序列表

Python - 将两个单列列表合并为一个双列列表并打印

Python:如何将两个嵌套列表合并为一个嵌套列表

如何将两个列表合并为一个长列表?

如何将两个客户列表合并为一个列表

如何将两个列表合并为一个列表?

将两个值添加到列表中的一个键并将列表添加到字典中

遍历两个列表并添加到结果列表将返回一个空列表。为什么?

将json文件中的两个字段添加到一个列表中,将两者都转换为浮点数,将其中一个字段转换为负数

R将两个列表合并为一个,从每个列表中交替绘制元素

将两个列表合并成一个单独的列表

在两个列表上执行查找并根据条件将第二个列表中的元素添加到第一个列表

Python:在一个文件行中检索两个单独的列表值

如何将一个列表元素分别添加到 2 个列表中?

批量将两个文本文件合并为一个文件

将两个文件合并为一个文件

将两个不同长度的文件合并为一个文件

如何将redux添加到同一个文件中的两个功能组件?

用Java将数据文件添加到两个单独的2D数组中