Monday, March 26, 2012

CIS 217: Array Assignment [Name Search]

The name search assignment was cake.
  1. Create a StreamReader object.
  2. Read BoyNames.txt file into memory.
  3. Check to see if it matches the name being searched for by the user.
  4. Display the results.
  5. Do the same for GirlNames.txt.
_Load event of the main form, calls methods to load boy and girl names from text files.
private void frmMain_Load(object sender, EventArgs e)
        {
            LoadBoyNames();
            LoadGirlNames();
        }

LoadBoyNames() loads the names for boys from the file.  Loading the names for girls was the same, obviously...just change the name of the method and file, etc.
public void LoadBoyNames()
        {
            try
            {
                // Index used in loops.
                int index = 0;

                // Declare StreamReader.
                StreamReader inputFile;

                // Open file and get SR obj.
                inputFile = File.OpenText("BoyNames.txt");

                // Read contents of BoyNames.txt
                while (index < boyNames.Length && !inputFile.EndOfStream)
                {
                    boyNames[index] = inputFile.ReadLine();
                    index++;
                }

                // Close the file.
                inputFile.Close();
            }
            catch
            {
            }
        }

SearchNames() method...passes reference to array, determines if current array item is the same as what the user entered (passed by value).
private int SearchNames(string[] sArray, string value)
        {
            bool found = false;
            int index = 0;
            int position = -1;

            // Search the array.
            while (!found && index < sArray.Length)
            {
                if (sArray[index] == value)
                {
                    found = true;
                    position = index;
                }

                index++;
            }

            return position;
        }

And finally, the Search button. 
private void btnSearch_Click(object sender, EventArgs e)
        {
            lbResults.Items.Clear();

            if (SearchNames(boyNames, txtBoyName.Text) != -1)
            {
                // MessageBox.Show("Found boy name!");
                lbResults.Items.Add("Boy found:\t" + txtBoyName.Text);
            }
            if (SearchNames(girlNames, txtGirlName.Text) != -1)
            {
                // MessageBox.Show("Found girl name!");
                lbResults.Items.Add("Girl found:\t" + txtGirlName.Text);
            }
        }

Piece of cake ;-)

2 comments:

  1. Cannot seem to get mine working. The line

    while (!found && index < sArray.Length)
    {
    seems to have a flaw. What is sArray? what arrays did you use for boys and girls? is that "boyNames" I keep returning an error on mine.

    ReplyDelete
    Replies
    1. Hello! Wow, I wrote this over three years ago, and completely forgot about it! I'm assuming you're talking about line 1 in the third picture:

      private int SearchNames(string[] sArray, string value)

      Basically, what this says is that you're creating a new method called SearchNames that returns an INT, and it accepts two things: a string array, and a string. I'm not sure how much programming experience you have, but the names "sArray" and "value" are arbitrary in this case: we could have called the string array "spaghettiArray" if we wanted to.

      Say, for example, we have an array of strings called "boyNames[]", and a name we want to find "Adam". We would pass our array into our method like this:

      int Found = SearchNames(Boys[], "Adam");

      That passes our boyNames[] (string array) and "Adam" (string) into the method "SearchNames". So it calls our SearchNames method, and that says "OK, well, I expect to get two things: a string array that I will call "sArray", and a string that I will call "value". Did I get these? The answer will be yes, of course, because we passed "boyNames[]" and "Adam" into those two spots.

      So inside of the method, we don't refer to Boys[] and "Adam" directly: we refer to them by what they're known as inside of the method: "sArray[]" and "value".

      Could I have named it better? Absolutely, but this was posted 9 months before I got my first programming job ;-) lol

      Let me know if you need more help.

      Delete