Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Streams and Data Processing Parsing Data Working with Floating-Point Types

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

Why don't I see the "dot" in the PossessionPercent property at runtime?

Hello, I have followed the code in the video, but when running it, i don't get the dot in the PossesionPercent property. For example, if my SoccerResult.csv file has the values: 8/26/2015 05:30 PM,Chicago Fire,Home,3,16,5,11,48.68 when running the program, I get: {01.01.0001 00:00:00} 16 3 Home 4868 11 5 "Chicago Fire"

however, the "4868" should display "48.68", but that doesn't happen. Any ideas why? Many thanks in advance!

Steven Parker
Steven Parker
231,007 Points

You'll need to share your code to make an analysis possible.

And when posting code, always use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: Or watch this video on code formatting.

You can also share an entire workspace project if you make a snapshot of your workspace and post the link to it here.

3 Answers

Antonio Adame
Antonio Adame
3,532 Points
double possessionPercent;
 if (double.TryParse(values[7], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out possessionPercent))
 {
       gameResult.PossessionPercent = possessionPercent;
 }
Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

Right, Sorry! I forgot about that... The code: Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SoccerStats
{
    class Program
    {
        static void Main(string[] args)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory = new DirectoryInfo(currentDirectory);
            var fileName = Path.Combine(directory.FullName, "SoccerGameResults.csv");
            var fileContents = ReadSoccerResults(fileName);


        }

        public static string ReadFile(string fileName) {
            using (var reader = new StreamReader(fileName)) {
                return reader.ReadToEnd();
            }
        }

        public static List<GameResult> ReadSoccerResults(string fileName) {
            var soccerResults = new List<GameResult>();
            using (var reader = new StreamReader(fileName)) {
                string line = "";
                reader.ReadLine();
                while ((line = reader.ReadLine()) != null) {
                    var gameResult = new GameResult();
                    string[] values = line.Split(',');

                    DateTime gameDate;
                    if (DateTime.TryParse(values[0], out gameDate)) {
                        gameResult.GameDate = gameDate;
                    }
                    gameResult.TeamName = values[1];
                    HomeOrAway homeOrAway;
                    if (Enum.TryParse(values[2], out homeOrAway)) {
                        gameResult.HomeOrAway = homeOrAway;
                    }
                    int parseInt;
                    if (int.TryParse(values[3], out parseInt)) {
                        gameResult.Goals = parseInt;
                    }
                    if (int.TryParse(values[4], out parseInt))
                    {
                        gameResult.GoalAttempts = parseInt;
                    }
                    if (int.TryParse(values[5], out parseInt))
                    {
                        gameResult.ShotsOnGoal = parseInt;
                    }
                    if (int.TryParse(values[6], out parseInt))
                    {
                        gameResult.ShotsOffGoal = parseInt;
                    }

                    double possessionPercent;
                    if (double.TryParse(values[7], out possessionPercent)) {
                        gameResult.PossessionPercent = possessionPercent;
                    }

                    soccerResults.Add(gameResult);
                }
            }
            return soccerResults;
        }
    }
}

GameResults.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoccerStats
{
    public class GameResult
    {
        public DateTime GameDate { get; set; }
        public string TeamName { get; set; }
        public HomeOrAway HomeOrAway { get; set; }
        public int Goals { get; set; }
        public int GoalAttempts { get; set; }
        public int ShotsOnGoal { get; set; }
        public int ShotsOffGoal { get; set; }
        public double PossessionPercent { get; set; }
        public double ConversionRate { get
            {
                return (double)Goals / (double)GoalAttempts;
            }
        }
    }

    public enum HomeOrAway{
        Home = 10,
        Away = 20
    }
}

Steven Parker
Steven Parker
231,007 Points

I see where the results are loaded into the "fileContents" variable, but I don't see any kind of output statement that would display the results. Is there still some code not shown?

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

Hello Steven,

I did not output the code yet, I did exactly as in the video. I just use a breakpoint in Program.cs in the return soccerResults; line, and then I previewed the values in Visual Studio. (I'm sorry if I'm ambiguous but I don't know the term for that kind of thing. Basically, I did the same thing as Carling Kirk in this video https://teamtreehouse.com/library/calculate-a-statistic at 03:44. Hope this helps explaining). Thank you for your help!