0

Some Background:

I'm currently learning C# and working on a ChatBot project. The Chatbot will learn from user input, by parsing each user inputted sentence, and placing each word into a dictionary with each word in the sentence as the key and the word that follows it as a value in the dictionary.

My First stumbling block is trying to loop through the string to place the words into the dictionary.

My Code:

class Program
{
    static string userInput;
    static string val;
    static string key;

    static Dictionary<string, string> dict = new Dictionary<string, string>();



    static void Main(string[] args)
    {

        userInput = Console.ReadLine();

        string[] wordbits = userInput.Split(' ');



        for (int i = 0; i < wordbits.Length; i++)
        {
            key = wordbits[i];
            for (int j = 0; j < wordbits.Length; j++)
            {
                val = wordbits[(j + 1)];
            }

            dict.Add(key, val);
        }



    }
}

The error I'm getting with this is IndexOutOfRangeException, which I assume is because the loop is looking for a word after the last word in the sentence, that doesn't exist.

Any suggestions on how to fix this?

2
  • try not to include the last word in your for loop and add it alone after the loop Commented Nov 20, 2013 at 10:38
  • The whole this is quite simple like this: wordbits.ToDictionary(x => x, x => wordbits[wordbits.Length - 1]) but I'm not sure you're after that. Or just wordbits.ToDictionary(x => x, x => wordbits.Last()) Commented Nov 20, 2013 at 10:48

2 Answers 2

1
 for (int j = 0; j < wordbits.Length; j++)
 {
     val = wordbits[(j + 1)];
 }

this won't work, you can change to:

 for (int j = 0; j < wordbits.Length-1; j++)
 {
     val = wordbits[(j + 1)];
 }

or just change (but this will change logic):

 val = wordbits[j];

because you are in last iteration accessing wordbits[wordbits.Length] you are getting exception, arrays are indexed from 0 to length-1

EDIT:

ok, I get it, you are getting values like: key value key value key value, change your logic to:

    for (int i = 0; i < wordbits.Length-1; i+=2)
    {
        key = wordbits[i];
        val = wordbits[(i + 1)];

        dict.Add(key, val);
    }

it will add keys and values to your dictionary. In your logic inner for is looping through all values of wordbits again, so it won't work, for loop is not needed there.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need such many loops here, just pass in the key, if it exists, the value will be updated, otherwise the new entry will be added:

userInput = Console.ReadLine();
var matches = Regex.Matches(userInput,@"\w+\s+\w+");
foreach(Match m in matches){
  if(m.Success){
    var w = m.Value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    dict[w[0]] = w[1];
  }
}

Note that, the code doesn't check for invalid input from user. All the input should be like this:

word1 value1 word2 value2 word3 value3 ...
//after collecting matches, we will the following matches:
word1 value1
word2 value2
word3 value3
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.