4 Replies - 1227 Views - Last Post: 01 December 2012 - 03:10 PM Rate Topic: -----

#1 Carl_Smith_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 12

Problem : Sequence contains no elements

Posted 01 December 2012 - 02:20 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.

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

                }

            }
        }
    }
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Problem : Sequence contains no elements

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,673
  • Joined: 02-June 10

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 02:41 PM

What are the actual run-time values of:
pair.Key
pair.Vlaue
Was This Post Helpful? 0
  • +
  • -

#3 Carl_Smith_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 12

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:01 PM

View PosttlhIn`toq, on 01 December 2012 - 02:41 PM, said:

What are the actual run-time values of:
pair.Key
pair.Vlaue



Not sure , im only a beginner ( they need to be set?) or something else?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:01 PM

Why are lines 52-61 in a {} block?

And the error is telling you there is nothing in pair.Value.

This post has been edited by Momerath: 01 December 2012 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,673
  • Joined: 02-June 10

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:10 PM

View PostCarl_Smith_93, on 01 December 2012 - 04:01 PM, said:

View PosttlhIn`toq, on 01 December 2012 - 02:41 PM, said:

What are the actual run-time values of:
pair.Key
pair.Vlaue



Not sure , im only a beginner ( they need to be set?) or something else?


Well - yeah, they have to have a value if you want to do something with them. This comes from your variableList variable. Did you put anything in the list?



: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#6 Carl_Smith_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 12

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:40 PM

View PostMomerath, on 01 December 2012 - 03:01 PM, said:

Why are lines 52-61 in a {} block?

And the error is telling you there is nothing in pair.Value.

Was just a mistake , nothing that will effect the program anyways.



Well - yeah, they have to have a value if you want to do something with them. This comes from your variableList variable. Did you put anything in the list?


-- The variableList , are meant to the variables such as R,H,P etc with the values that are associated to them. All the var's + values 'should' go into the list as the program goes by due to the code used.





: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

I'll take a look using breakpoints(first ill froogle how to :D)
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



[/quote]
Was This Post Helpful? 0
  • +
  • -

#7 Carl_Smith_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 12

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:40 PM

View PostMomerath, on 01 December 2012 - 03:01 PM, said:

Why are lines 52-61 in a {} block?

And the error is telling you there is nothing in pair.Value.

Was just a mistake , nothing that will effect the program anyways.



Well - yeah, they have to have a value if you want to do something with them. This comes from your variableList variable. Did you put anything in the list?


-- The variableList , are meant to the variables such as R,H,P etc with the values that are associated to them. All the var's + values 'should' go into the list as the program goes by due to the code used.





: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

I'll take a look using breakpoints(first ill froogle how to :D)
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



[/quote]
Was This Post Helpful? 0
  • +
  • -

#8 Carl_Smith_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 12

Re: Problem : Sequence contains no elements

Posted 01 December 2012 - 03:41 PM

View PostMomerath, on 01 December 2012 - 03:01 PM, said:

Why are lines 52-61 in a {} block?

And the error is telling you there is nothing in pair.Value.

Was just a mistake , nothing that will effect the program anyways.



Well - yeah, they have to have a value if you want to do something with them. This comes from your variableList variable. Did you put anything in the list?


-- The variableList , are meant to the variables such as R,H,P etc with the values that are associated to them. All the var's + values 'should' go into the list as the program goes by due to the code used.





: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

I'll take a look using breakpoints(first ill froogle how to :D)
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



[/quote]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1