learning to code - dictionary question

R

Ryan

Hello,

I'm am learning to program and hoping someone could help me with my code question.

Below I have pasted the C# dictionary code example I am working on. The intent of the program is to ask a user a question, read their response, and respond back to the user. This is a counsel application written in C#, usingthe visual studio 2010 express edition.

Reviewing the code below, where I am stuck is on this line:

Console.WriteLine("my response is {0}",dict[myInput]);

What I would like the program to do is read the response the user entered, find the key within the dictionary, and respond back to the user based on the value for that key.

I believe what is incorrect above is where I am trying to pass in the string value from the myInput variable, into the dictionary.

If the program worked correctly - it would ask the user their name, if the user typed "tara", it would respond back "Hi Tara!" (based on the string value in the dictionary).


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings.
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");

//Introduction
Console.WriteLine("What is your name?");

//Read Answer
String myInput = Console.ReadLine();

//Respond
Console.WriteLine("my response is {0}",dict[myInput]);

//Stop
Console.ReadLine();
}
}
}
 
A

Alf P. Steinbach

Hello,

I'm am learning to program and hoping someone could help me with my code question.

Below I have pasted the C# dictionary code example I am working on. The intent of the program is to ask a user a question, read their response, and respond back to the user. This is a counsel application written in C#, using the visual studio 2010 express edition.

Reviewing the code below, where I am stuck is on this line:

Console.WriteLine("my response is {0}",dict[myInput]);

What I would like the program to do is read the response the user entered, find the key within the dictionary, and respond back to the user based on the value for that key.

I believe what is incorrect above is where I am trying to pass in the string value from the myInput variable, into the dictionary.

If the program worked correctly - it would ask the user their name, if the user typed "tara", it would respond back "Hi Tara!" (based on the string value in the dictionary).


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings.
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");

//Introduction
Console.WriteLine("What is your name?");

//Read Answer
String myInput = Console.ReadLine();

//Respond
Console.WriteLine("my response is {0}",dict[myInput]);

//Stop
Console.ReadLine();
}
}
}

<example>
d:\dev\test\csharp> nedit foo.cs

d:\dev\test\csharp> csc foo.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.17020
Copyright (C) Microsoft Corporation. All rights reserved.


d:\dev\test\csharp> foo
What is your name?
Tara
my response is Hi Tara!


d:\dev\test\csharp> _
</example>

Are you sure that you typed in "Tara" with uppercase first letter?

Cheers & hth.,

- Alf
 
A

Arne Vajhøj

Reviewing the code below, where I am stuck is on this line:

Console.WriteLine("my response is {0}",dict[myInput]);

What I would like the program to do is read the response the user entered, find the key within the dictionary, and respond back to the user based on the value for that key.

I believe what is incorrect above is where I am trying to pass in the string value from the myInput variable, into the dictionary.

If the program worked correctly - it would ask the user their name, if the user typed "tara", it would respond back "Hi Tara!" (based on the string value in the dictionary).
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");

No.

If the user typed "Tara" (uppercase T) then it would respond
back "Hi Tara!".

Arne
 
R

Ryan

Hello,

Thank you for the responses. You are both correct. Typing "Tara" with uppercase worked. This appears now that I have an error in my design. It should ignore upper or lower case when searching through the directory.

Any ideas?
 
A

Arne Vajhøj

Thank you for the responses. You are both correct. Typing "Tara"
with uppercase worked. This appears now that I have an error in my design. It
should ignore upper or lower case when searching through the directory.

Any ideas?

The simple solution would be to call ToUpper on key when you add to
the Dictionary and when doing lookup.

A more complex but also somewhat better solution would be to
wrap the key in a class with overridden Equals and HashCode
to give the desired functionality.

Arne
 
R

Ryan

I did a little research and added:

StringComparer.InvariantCultureIgnoreCase

to "Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);"

This works. But i'm not sure as to how this enabled this to happen? Any ideas?

Thank you.




using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings.
Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");

//Introduction
Console.WriteLine("What is your name?");

//Read Answer
String myInput = Console.ReadLine();

/*Respond */
String myResponse = dict[myInput];
Console.WriteLine("Here is my response: {0}", myResponse);

//Stop
Console.ReadLine();
}
}
}
 
A

Arne Vajhøj

I did a little research and added:

StringComparer.InvariantCultureIgnoreCase

to "Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);"

This works. But i'm not sure as to how this enabled this to happen? Any ideas?

Ah.

When supplied the Dictionary use the Equals(object,object) and
HashCode(object) methods of the StringComparer instead of
the methods of the objects.

This saves you the work of coding something yourself.

Arne
 
R

Ryan

Expanding on what I have already, I'm wondering how I can make the program select the next question within my 'question' dictionary. Please review below under /* NEED HELP HERE:




using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings for Questions.
Dictionary<string, string> question = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
question.Add("1", "What is your name?");
question.Add("2", "How are you feeling today?");

// Create and initilize dictionary of strings for Responses.
Dictionary<string, string> response = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
response.Add("Tara", "Hi Tara!");
response.Add("Ryan", "Hi Ryan!");


/* NEED HELP HERE: What i would like the following statement to do is to select the next question in the 'question' dictionary.
Currently, it incorreclty only selects the first question. */

// Ask Question
string myQuestion = question["1"];
Console.WriteLine("Question: {0}", myQuestion);

// Read Answer
String myInput = Console.ReadLine();

// Response
String myResponse = response[myInput];
Console.WriteLine("Response: {0}", myResponse);

//Stop
Console.ReadLine();
}
}
}
 
A

Arne Vajhøj

Expanding on what I have already, I'm wondering how I can make the program select the next question within my 'question' dictionary. Please review below under /* NEED HELP HERE:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings for Questions.
Dictionary<string, string> question = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
question.Add("1", "What is your name?");
question.Add("2", "How are you feeling today?");

List<string> question = new List<string>();
question.Add("What is your name?");
question.Add("How are you feeling today?");
// Create and initilize dictionary of strings for Responses.
Dictionary<string, string> response = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
response.Add("Tara", "Hi Tara!");
response.Add("Ryan", "Hi Ryan!");


/* NEED HELP HERE: What i would like the following statement to do is to select the next question in the 'question' dictionary.
Currently, it incorreclty only selects the first question. */
// Ask Question
string myQuestion = question["1"];

foreach(string myQuestion in question)
{
Console.WriteLine("Question: {0}", myQuestion);

// Read Answer
String myInput = Console.ReadLine();

// Response
String myResponse = response[myInput];
Console.WriteLine("Response: {0}", myResponse);
}

//Stop
Console.ReadLine();
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top