Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem : Sequence contains no elements

$
0
0
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.

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 peices using the semi colon as the point to seperate each char.

        List<int> numbers = new List<int>();
        // This creates a new list.

        //  Action parse = delegate { };

        //   Action flush = delegate
        //   {
        //      parse();
        //      numbers.Clear();
        //  };

        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();
                    }

                }

            }
        }
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>