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>>>();
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();
if (!Region.ContainsKey(main_area))
Region[main_area] = new Dictionary<string, IDictionary<string, int>>();
if (!Region[main_area].ContainsKey(cluster))
{
Region[main_area][cluster] = new Dictionary<string, int>();
Region[main_area][cluster][excDays] = 0;
}
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);
}
}
}
Please help me on this. The error comes when recLine reached
the 8 records onward.
Thank you in advance and God speed...