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 trialMohamed Monir
22,362 Pointsi tray many code and nothing solve
in this i tray many code but no one work i use tray cach , if , remove but nothing work
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
public void AddWord(string word)
{
if (WordCount["rock"] > 0)
{
WordCount.Remove("rock");
WordCount.Add("rock",2);
}
}
}
}
3 Answers
Steven Parker
231,198 PointsYou won't need "try...catch" for this challenge. But there's three basic things your code needs to do:
- check to see if the word is already in the dictionary (the "ContainsKey" method can be handy for this)
- if it exists, increase the count it already has by 1
- if it does not exist, add it with a count of 1
Stephen Mooney
2,794 PointsYou are trying to check a key-value pair that you have not added to the dictionary yet. I get this error if I run your code.
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
To check a dictionary you first have to add something to it
have a look at my console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
public static Dictionary<string, int> WordCount = new Dictionary<string, int>();
public static void Main(string[] args)
{
WordCount.Add("rock", 1);
AddWord("rock");
}
public static void AddWord(string word)
{
if (WordCount[word] > 0)
{
WordCount.Remove(word);
WordCount.Add(word, 2);
}
}
}
}
As you can see i added the first key-value pair then i was able to check it.
Steven Parker
231,198 PointsTo avoid any potential confusion, the code shown here is not a solution for the challenge.
And for those working on the challenge, note that the instructions say the added code should (only) go into the "AddWord" method.
Mohamed Monir
22,362 Pointsi tray this code but is not working
Stephen Mooney
2,794 Pointsim not trying to give the answer for the solution. i simply demonstrated where his code is going wrong. It is perfectly correct.
firstly he did not post what the challenge was asking for. secondly i identified the exception occurring - so he can learn to figure problems out for himself. thirdly i identified the work he was trying to check to see if it was greater than zero did not exist in the first place. fourth and finally i used a console app because it was easier for me to work it out and demonstrate these simple points.
Why not try teaching someone how to learn to problem solve that solving the problem for them? (Moderator redacted)
Steven Parker
231,198 PointsYou can use the "View Challenge" button in the upper right of the window to see for yourself exactly what the challenge is asking for.
I am dedicated to helping students learn to solve problems themselves, and resistant to giving code answers. I primarily provide hints as I have done here. But I try to give hints that will help the student move towards a successful solution.
Please consider that showing someone code that does something other than what is required by the challenge could possibly cause additional confusion.
Jennifer Nordell
Treehouse TeacherStephen Mooney I have redacted the ending bit of your "answer" to remove what I consider to be targeted harassment of another student. Please note that name-calling will not be tolerated in the Community. I highly encourage you to read the Code of Conduct that all members of the Community are to abide by. Being a part of this community means being excellent to each and every person you have contact with, whether you agree with their style of helping or not.