I'm currently making a program, that will read data from a file, that contains a number different variables. These being, D - Date , T, R, P and H which are just values. The program will need to add up all the 'T' values, 'R' values , including the P and H values separately. With the same format and outputting the average of each value. ( I'm sure most of it has been done already via the code I currently have). Though, their are some problems that arise. (I'll explain, further down.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//Class Libary IO has been added to the list
class Program
{
public static void Main()
{
//FileStream file = new FileStream(@"c:\CSharp\CW1_2012.txt", FileMode.Open);
// StreamReader read = new StreamReader(file);
// The file will be located and and opened for use further into the program.
Dictionary<string, List<int>> variableList = new Dictionary<string, List<int>>();
string whiteSpace = new string(File.ReadAllText(@"c:\CSharp\CW1_2012.txt").Where(dataFile => !char.IsWhiteSpace(dataFile)).ToArray());
// This strips the file of white space.
string[] fields = whiteSpace.Split(new char[] { ':' }, StringSplitOptions.None);
// This splits the file up into pieces using the semi colon as the point to seperate each char.
List<int> numbers = new List<int>();
// This creates a new list.
foreach (string field in fields
// This looks through all of the fields.
{
if (char.IsDigit(field.FirstOrDefault()))
{
numbers.Add(Convert.ToInt32(field));
}
else
{
List<int> currentlist = null;
if (!variableList.TryGetValue(field, out currentlist))
{
currentlist = new List<int>();
variableList.Add(field, currentlist);
}
{
currentlist.AddRange(numbers);
foreach (var pair in variableList)
{
Console.WriteLine("{0} {1}", pair.Key, pair.Value.Average());
Console.ReadLine();
}
The problem arises at this section of the program. I get an error saying 'Sequence contains no elements' I'm unsure what to do from here. I have looked for other solutions and tryed to figure what is wrong but as you can see I havent found one just yet xD
When I try to debugg it , it simply flashes the cmd box for a second so, now im not sure if the program is doing what it is meant to do.
}
}
}
}
--- Second small problem.
If i can get a fix for the current code, how would I get it to export the data + the produced averages into an excel file? I have this code I would like to use if possible.
Bookmarks