Home > Software engineering >  Is there a way to match user input string to string in a list and return the index?
Is there a way to match user input string to string in a list and return the index?

Time:12-18

I am trying to figure out how to create: A loop that iterates through a list and then displays the index of the item in the list that contains matching text on the screen. Needs to match users input. What I have so far:

List<string> uniqueItems = new List<string>();
        { "Urn of Amen-Ra", "Dyanises Sword", "Pallades Diary", "Ring of Solomon", "Barbar Tea Set" };
        
        Console.WriteLine("Please input text to search in the list: ");
        string userItem = Console.ReadLine();
        Console.WriteLine("You have ");

        for (int d = 0; 0 < uniqueItems.Count; d  )
        {
            Console.WriteLine(uniqueItems[d]);
            break;
        }

CodePudding user response:

Just use IndexOf:

List<string> uniqueItems = new List<string>()
{
    "Urn of Amen-Ra", "Dyanises Sword", "Pallades Diary",
    "Ring of Solomon", "Barbar Tea Set"
};

Console.WriteLine("Please input text to search in the list: ");
string userItem = Console.ReadLine();
Console.WriteLine("You have ");
Console.WriteLine(uniqueItems.IndexOf(userItem));

CodePudding user response:

You seem to have asked two slightly different questions. One is about finding the index of an item, the other is about knowing if an item exists or not

If you want to write these yourself, you need to do some test inside your loop

    for (int d = 0; 0 < uniqueItems.Count; d  )
    {
        Console.WriteLine(uniqueItems[d]);
        break;
    }

First off you've written 0 < uniqueItems.Count which is always true so this loop will run until the program crashes trying to access an item after the end of the list. Your loop test should read d < ...

But to the point about searching, you need to actually check in the loop whether the item you're looking at is the item you're looking for:

    for (int d = 0; 0 < uniqueItems.Count; d  )
    {
        string lookingAt = uniqueItems[d];
        if(lookingAt == userItem){
          Console.WriteLine("Found it at index "   d);
          break; //exit the loop early
        } else {
          Console.WriteLine("Didn't find it at index "   d);
        }
    }

This will print out messages that it didn't find it, until it does m, and then it stops. It is, however, not that useful because as soon as we break out of the loop we lose all knowledge of where the item was, because d disappears when we leave the loop

Instead let's have a variable outside the loop that we can set if we find it. We'll set this variable to -1 to start with because there is no index -1 in a list, so if it stays at -1 when the loop is over we never found it

    int foundAt = -1; 
    for (int d = 0; 0 < uniqueItems.Count; d  )
    {
        string lookingAt = uniqueItems[d];
        if(lookingAt == userItem){
          foundAt = d;
          break; //exit the loop early
        } 
    }

We don't need that else now because we aren't really interested in items we aren't looking for.

When the loop is over we can examine foundAt to see if it's -1. If it is, we know we never found what we were looking for

if(foundAt == -1)
  Console.WriteLine("The list doesn't have the droid you're looking for");
else
  Console.WriteLine("The droid you're looking for is in list index "   foundAt);

Microsoft already wrote this code for you in the form of IndexOf and Contains methods but if this is an academic exercise your supervisor will want to see you assemble it yourself. To this end I'm not going to give the whole code but have a play with putting together the snippets given and satisfy yourself that you know how they work

  •  Tags:  
  • c#
  • Related