Carl

Carl

  • NA
  • 3
  • 4.3k

Problem : Sequence Contains No Elements

Dec 1 2012 4:26 PM
Hello. 

So, Im having trouble resolving a problem with my current program.

The basics of it is, that it reads a data file, takes the data from the file, adds each 'individual' variable values together, and outputs the average of them on the cmd prompt box.Ignoring the "D" tag which is the date that will only be used for another part of the program( to be added when I find a fix for this)

The problem appears at this line of code, as the title says 'Sequence contains no elements'. 
Console.WriteLine("{0} {1}", pair.Key, pair.Value.Average());

Parse/flush are not used at the moment as they have no effect on the program( for later use)

Thank you for responses in advance.


[code]
using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.IO;
05 
06//Class Libary IO has been added to the list
07 
08class Program
09{
10 
11    public static void Main()
12    {
13        //FileStream file = new FileStream(@"c:\CSharp\CW1_2012.txt", FileMode.Open);
14        // StreamReader read = new StreamReader(file);
15        // The file will be located and and opened for use further into the program.
16 
17        Dictionary<string, List<int>> variableList = newDictionary<string, List<int>>();
18        string whiteSpace = newstring(File.ReadAllText(@"c:\CSharp\CW1_2012.txt").Where(dataFile => !char.IsWhiteSpace(dataFile)).ToArray());
19        // This strips the file of white space.
20 
21        string[] fields = whiteSpace.Split(new char[] { ':' }, StringSplitOptions.None);
22        // This splits the  file up into peices using the semi colon as the point to seperate each char.
23 
24        List<int> numbers = new List<int>();
25        // This creates a new list.
26 
27        //  Action parse = delegate { };
28 
29        //   Action flush = delegate
30        //   {
31        //      parse();
32        //      numbers.Clear();
33        //  };
34 
35        foreach (string field in fields
36        // This looks through all of the fields.
37        {
38            if (char.IsDigit(field.FirstOrDefault()))
39            {
40                numbers.Add(Convert.ToInt32(field));
41            }
42            else
43            {
44                List<int> currentlist = null;
45 
46                if (!variableList.TryGetValue(field, out currentlist))
47                {
48                    currentlist = new List<int>();
49                    variableList.Add(field, currentlist);
50                }
51 
52                {
53                    currentlist.AddRange(numbers);
54 
55                    foreach (var pair in variableList)
56                    {
57                        Console.WriteLine("{0} {1}", pair.Key, pair.Value.Average());
58                        Console.ReadLine();
59                    }
60 
61                }
62 
63            }
64        }
65    }
66}
[/code]

Answers (2)