Increasing the value of a key in a method

Increasing the value if the key already exists

Page 1 of 1

2 Replies - 885 Views - Last Post: 07 January 2010 - 07:05 AM Rate Topic: -----

#1 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Increasing the value of a key in a method

Posted 07 January 2010 - 12:48 AM

Hi folks,

I am currently trying to implement a method which shall create a new object SortedList and write keys and values to a .txt file. The idea is that this whole method is supposed to Count specific words out of a .txt file and return them to main.

It works currently and finds 779 unique words and returns them to the main program again which handles the output , however i don't quite grip how i can increase the value of an already existing key. The point being if the word(key) already exist it is only supposed to increment the int value.

Frequency Table example done right if more words of the same exists:

Key / Value

Hey / 3
Yes / 2

Frequency Table as it looks now, no matter how many words are the same:

Key / Value

Hey / 1
Yes / 1


This is how the method currently looks;

		public static IDictionary<string, int> CountWords(string[] words)
		{
			IDictionary<string, int> dic = new SortedList<string, int>();
			foreach (string word in words)
			{
				if (dic.ContainsKey(word))
				{
					// Not sure what to add here :(
				}
				else
				{
					dic.Add(word,1);
				}
			}
			return dic;
			throw new Exception("The method is not implemented.");
		}



Appreciate any help or hints, happy new year.

Is This A Good Question/Topic? 0
  • +

Replies To: Increasing the value of a key in a method

#2 TravisP   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 06-January 10

Re: Increasing the value of a key in a method

Posted 07 January 2010 - 12:58 AM

You might do something like:

dic.Value += 1 within the if statement.

Might want to test around with it a bit, I'm still learning C# too!

This post has been edited by TravisP: 07 January 2010 - 01:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Increasing the value of a key in a method

Posted 07 January 2010 - 07:05 AM

Something like this should do. You also need to decide if the same word starting in upper or lower is counted as two different words or the same word. If anything unclear, feel free to ask.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlsAutoGenerator
{
	public partial class Form1 : Form
	{
		private string[] words = null;

		public Form1()
		{
			InitializeComponent();
			InitWordsArray();

			IDictionary<string, int> myDic = CountWords(words);
		}

		private void InitWordsArray()
		{
			words = new string[] { "hey", "yes", "hey", "hello", "yes" };
		}

		private IDictionary<string, int> CountWords(string[] words)
		{
			IDictionary<string, int> dic = null;

			if (words != null)
			{
				dic = new Dictionary<string, int>();
				foreach (string word in words)
				{
					if (!dic.ContainsKey(word))
					{
						dic[word] = 1;
					}
					else
					{
						dic[word] += 1;
					}
				}
			}

			return dic;
		}
	}
}




Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1