IDictonary错误,请帮助我,先生 [英] Error in IDictonary, please help me sir

查看:67
本文介绍了IDictonary错误,请帮助我,先生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



祝大家有美好的一天!
我使用字典来获取键和值,但是
我收到一个我为什么不总是出现的错误:
字典中没有给定的键"

但在我的输入文本文件中,该键存在.
这是我的示例输入文件:

MAIN_AREA集群EXC_TO_COM_DAYS
====================================
CL1 BATAAN 1
CL1 BULACAN1A 1
NL ABRA 2
GMA1 BULACAN4 3
NL ILOCOS SUR 4
GMA2 MANILA1 7
CL1 BATAAN 1
GMA1 BULACAN4 9
CL1 BULACAN1A 2

这是我完整的代码...

Hi,

Good day to all!
I used the Dictionary to get the keys and values, but
I got an error which I don''t why always appearing:
"THE GIVEN KEY WAS NOT PRESENT IN THE DICTIONARY"

but in my input text file, that key is present.
Here''s my sample input file:

MAIN_AREA CLUSTER EXC_TO_COM_DAYS
=========== =========== ================
CL1 BATAAN 1
CL1 BULACAN1A 1
NL ABRA 2
GMA1 BULACAN4 3
NL ILOCOS SUR 4
GMA2 MANILA1 7
CL1 BATAAN 1
GMA1 BULACAN4 9
CL1 BULACAN1A 2

Here''s my complete code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace Clustering_Test
{
    class Program
    {
        static int T24, T3, T4;
        static void Main(string[] args)
        {
            IDictionary<string, IDictionary<string, IDictionary<string, int>>> Region = new Dictionary<string, IDictionary<string, IDictionary<string, int>>>();
            // Reading input text file
            string dirFile = @"C:\FileDir\SAMPLE_INPUT2.txt";
            string recLine;
            int recCounter = 0;
            StreamReader srTable = new StreamReader(dirFile);
            while ((recLine = srTable.ReadLine()) != null)
            {
                recCounter++;
                if (recCounter < 3) continue;
                ParseLine(recLine, Region);
            }
            Dump(Region);
            Console.ReadKey();
        }
        private static void ParseLine(string recLine, IDictionary<string, IDictionary<string, IDictionary<string, int>>> Region)
        {
            if (recLine.Trim() == String.Empty)
                return;
            try
            {
                string main_area = recLine.Substring(0, 11).Trim();
                string cluster = recLine.Substring(12, 11).Trim();
                string excDays = recLine.Substring(24, 16).Trim();

                // adding cluster type key
                if (!Region.ContainsKey(main_area))
                    Region[main_area] = new Dictionary<string, IDictionary<string, int>>();
                // adding cluster name key
                if (!Region[main_area].ContainsKey(cluster))
                {
                    Region[main_area][cluster] = new Dictionary<string, int>();
                    Region[main_area][cluster][excDays] = 0;
                }

                // summing up total
                if (Region[main_area].ContainsKey(cluster))
                 Region[main_area][cluster][excDays] += 1;
                int excDays2 = Convert.ToInt32(excDays);
                if (excDays2 < 2)
                    T24++;
                else if (excDays2 > 1 && excDays2 < 4)
                    T3++;
                else if (excDays2 > 3)
                    T4++;

            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Failed to parse line \"{0}\", {1}", recLine, e.Message);
            }
        }
        private static void Dump(IDictionary<string, IDictionary<string, IDictionary<string, int>>> main_area)
        {
            foreach (string MainArea in main_area.Keys)
            {
                foreach (string Cluster in main_area[MainArea].Keys)
                {
                    foreach (string ExcDays in main_area[MainArea][Cluster].Keys)
                    {
                        Console.WriteLine("There are {0} {1} {2} named {3}", main_area[MainArea][Cluster][ExcDays], ExcDays, Cluster, MainArea);
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("Total 24hrs:   {0} ", T24);
            Console.WriteLine("Total 3days:   {0} ", T3);
            Console.WriteLine("Total 4days:  {0} ", T4);
        }
    } // End class
} // End namespace





请帮我.该错误来自recLine达到
8条记录开始.

预先感谢您,上帝的速度!...





Please help me on this. The error comes when recLine reached
the 8 records onward.

Thank you in advance and God speed...

推荐答案

您好,

我刚刚测试了您的代码.感谢您的完整列表:)

错误在这里:

Hello,

I just tested your code. Thanks for the full listing :)

The error is here:

// adding cluster name key
if (!Region[main_area].ContainsKey(cluster))
{
    Region[main_area][cluster] = new Dictionary<string, int>();
    Region[main_area][cluster][excDays] = 0;
}



仅在群集键不存在的情况下才创建[excDays]条目,因此具有相同群集键的所有其他条目都将没有excDays键.

以这种方式使用它,然后工作:



the [excDays] entry is only created in the case the cluster key does not exist, so all additional entries with the same cluster key will not have a excDays key.

use it this way, then it works:

// adding cluster name key
if (!Region[main_area].ContainsKey(cluster))
{
    Region[main_area][cluster] = new Dictionary<string, int>();
}
if (!Region[main_area][cluster].ContainsKey(excDays))
{
    Region[main_area][cluster][excDays] = 0;
}



更新:检查excDays是否已经存在,如果不存在,则>初始化为0.

错误搜索的提示:如果您正在开发(尤其是遇到错误),请不要使用任何try {} catch {}块.如果删除try-catch并开始调试,VisualStudio将为您提供错误的确切位置,并且您可以在错误点检查任何变量.这样,您会很快发现错误:)


希望这会有所帮助.

致以诚挚的问候,祝您编程愉快,
Stops



Update: check if the excDays already exists, if not => initialize with 0.

Tip for the error-searching: Don''t use any try {} catch {} blocks if you''re developing (especially if you get an error). If you remove the try-catch and start debugging, VisualStudio gives you the exact location of the error and you can inspect any variables at the point of error. This way, you will find the error pretty fast :)


Hope this helps.

Best regards and happy coding,
Stops


请向我解释为什么使用IDictionary< blabla>而不是Dictionary< blabla>.
please explain to me why you use IDictionary<blabla> and not Dictionary<blabla>.


这篇关于IDictonary错误,请帮助我,先生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆