2 Replies - 7949 Views - Last Post: 05 December 2011 - 08:41 AM Rate Topic: -----

#1 Viper786   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 30-November 11

I can't figure out how to search for items in 2 different Lists

Posted 02 December 2011 - 06:47 PM

I have a homework assignment that provides me with two text files (GirlNames.txt and BoyNames.txt) and we need to create an application that reads the contents of the two files into two separate arrays 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.

Now, I have most of it figured out, the only part I am having trouble with is when the user clicks submit, it only searches the BoyNames.txt file and I don't know how to make it search both lists at the same time. Can someone please help me out?

Here is my code, I added some comments to make it easier to understand what I'm doing:

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

        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.");
            }
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: I can't figure out how to search for items in 2 different Lists

#2 Viper786   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 30-November 11

Re: I can't figure out how to search for items in 2 different Lists

Posted 02 December 2011 - 07:15 PM

Never mind, I figured it out. I combined the two lists and used that in the search instead.

I added the following lines and now it works!

        List<string> combinedList = new List<string>();
        combinedList.AddRange(nameList);
        combinedList.AddRange(namelist2);



Just for future knowledge, is there another way to search two different lists without combining the lists after someone clicks the button?
Was This Post Helpful? 2
  • +
  • -

#3 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: I can't figure out how to search for items in 2 different Lists

Posted 05 December 2011 - 08:41 AM

Well, one other way to do is by having the file have specific symbols or letters splitting each string, and then declare an array that uses the Split() method. After that, you could have a while loop. Example code below:

TextReader tw = new TextReader("File.txt");
string lists = tw.ReadToEnd();
string[] list = lists.Split(',');
int cout = list.Count();
int num = 0;
while(num != cout)
{
   if(textBox1.Text.Equals(list[num].ToString())
   {
      MessageBox.Show("Found!"); 
      break;
   }
   else
   {
       num += 1;
   }
   if(num > cout)
   {
       MessageBox.Show("Could not find this person.");
       break;
   }
}



That is one way to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1