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; |
02 | using System.Collections.Generic; |
03 | using System.Linq; |
04 | using System.IO; |
05 |
06 | //Class Libary IO has been added to the list |
07 |
08 | class 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 | } |
CarlPosted Dec 2, 2012, 1:55 PM
I've fiddled and fixed the program to what I want it too do.
Though to clarify:
The program will read a file, extracting the data. Using chars such as R,P etc seperate chars and adding > averaging the numbers after each letter.
VulpesPosted Dec 2, 2012, 11:44 AM
For example, if I try the code with this single line file:
1 : 2 : 3
then the program produces no output as there are no key/value pairs in the dictionary.
However, if I try it with this file:
1 : 2 : a
then the output is :
a 1.5
because 'a' is the key and 1.5 is the average of 1 and 2.
I don't know whether this is what you were expecting but it is what the code actually does.