Ana Mae
Ana Mae

Reputation: 33

Arraylist search

SO I'm working on a program and I want to search for a certain word, but I'm not all too sure how to set up a search function. This is what I was attempting to use, but all it is doing is output whatever I type in. Any help with this is appreciated!

    static void searchWords(ArrayList arlWords)
    {
        string strInput;

        Console.Write("Search a Word: ");
        strInput = Console.ReadLine();
        arlWords.IndexOf(strInput, 0);
        Console.WriteLine("{0}", strInput);
    }

Upvotes: 1

Views: 87

Answers (2)

Eray Balkanli
Eray Balkanli

Reputation: 7960

Please try this:

static void searchWords(ArrayList arlWords)
{
  string strInput;
  bool check= false;
  Console.Write("Search a Word: ");
  strInput = Console.ReadLine();
  for (int i = 0; i < arlWords.Items.Count; i++)
  {
     if (arlWords.Items[i] == strInput )
        check = true;
  }
  if(check) Console.WriteLine("found");
  else Console.WriteLine("not found");
}

Upvotes: 1

Jouni Aro
Jouni Aro

Reputation: 2139

You have defined it to write 'strInput' so that it will write. Instead you should catch the result of 'IndexOf' and print that - or whether it is greater than 0 or not.

Upvotes: 1

Related Questions