Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnne Yeiser
19,291 PointsCannot figure out task 1 of C# Collections Sets-and-dictionaries/using-dictionaries-- "Code the AddWord method..."
so far, I have--- using System.Collections.Generic;
namespace Treehouse.CodeChallenges { public class LexicalAnalysis { public Dictionary<string, int> WordCount = new Dictionary<string, int>();
public void AddWord(string word)
{
List<string> output = new List<string>(output.Count);
foreach(KeyValuePair<string, int> code in WordCount())
{
WordCount[int.Value] = int.Key;
}
}
}
}
and it is not working.
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
public void AddWord(string word)
{
List<string> output = new List<string>(output.Count);
foreach(KeyValuePair<string, int> code in WordCount())
{
WordCount[int.Value] = int.Key;
}
}
}
}
2 Answers
Steven Parker
231,269 PointsYou've got a few issues to work on yet, here's a few hints:
-
List<string> output = new List<string>(output.Count)
You can't reference a variable while defining it -
foreach(KeyValuePair<string, int> code in WordCount())
You create code but never use it - also, WordCount is not a function (so no parentheses)
-
WordCount[int.Value] = int.Key
int is a type, not a variable - also, a dictionary would normally be indexed by a Key, not a Value
- you'll probably want to test if the word is already in the dictionary
- if word is not in the dictionary, you might add it with a value of 1
- otherwise, you probably want to increment the current value
Anne Yeiser
19,291 PointsI am still stumped. I'm sorry.