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 trialSanele Mogale
1,075 PointsPossessionPercent
I followed Carling's video exactly as she explained. Everything works when I debug. But the problem is that on my PossessionPercentage output on the debug, I get a 0 instead of getting the double value in my csv. Please check my 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);
////variable passing method
//var fileContents = ReadFile(fileName);
////
//string[] fileLines = fileContents.Split(new char[] { '\r','\n'},StringSplitOptions.RemoveEmptyEntries);
//foreach (var line in fileLines)
//{
// Console.WriteLine(line);
//}
//reading from data.txt
//var fileName = Path.Combine(directory.FullName, "data.txt");
//var file = new FileInfo(fileName);
//if (file.Exists)
//{
// using (var reader = new StreamReader(file.FullName))
// {
// Console.SetIn(reader);
// Console.WriteLine(Console.ReadLine());
// }
//}
///// return a list of fiels
/////
//var files = directory.GetFiles();//you can use a wildcard to find specific files using "*.txt"in the GetFIles() method
////returns a list of files in the directory
//foreach (var file in files)
//{
// Console.WriteLine(file.Name);
//}
Console.ReadKey();
}
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();//sort of throw it away for now
while ((line = reader.ReadLine()) != null)
{
//parsing game date from csv file into datetime object so we can store in gmae result class
var gameResult = new GameResult();//we assigning our date but we need to convert it to datetime first
string[] values = line.Split(',');
//first element
DateTime gameDate;
if (DateTime.TryParse(values[0], out gameDate))
{
gameResult.GameDate = gameDate;
}
//2nd element
gameResult.TeamName = values[1];
//3rd element
HomeOrAway homeOrAway;
if (Enum.TryParse(values[2], out homeOrAway))
{
gameResult.HomeOrAway = homeOrAway;
}
//3rd element
int parseInt;
if (int.TryParse(values[3], out parseInt))
{
gameResult.Goals = parseInt;
}
//4th element
if (int.TryParse(values[4], out parseInt))
{
gameResult.GoalAttempts = parseInt;
}
//5th element
if (int.TryParse(values[5], out parseInt))
{
gameResult.ShortsOnGoal = parseInt;
}
//6th element
if (int.TryParse(values[6], out parseInt))
{
gameResult.ShortsOffGoals = parseInt;
}
//7th element
double possessionPercent;
if (double.TryParse(values[7], out possessionPercent ))
{
gameResult.PossessionPercent = possessionPercent;
}
soccerResults.Add(gameResult);
}
}
return soccerResults;
}
}
}
GameResult.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 ShortsOnGoal { get; set; } public int ShortsOffGoals { get; set; } public double PossessionPercent { get; set; }
}
public enum HomeOrAway
{
Home,
Away
}
}
SoccerGameResults.csv
GameDate,TeamName,HomeOrAway,Goals,GoalAttempts,ShotsOnGoal,ShotsOffGoal,PosessionPercent 8/26/2015 05:30 PM,Chicago Fire,Home,3,16,5,11,48.68 8/26/2015 05:30 PM,New York Red Bulls,Away,2,12,3,9,51.32 8/26/2015 06:00 PM,Colorado Rapids,Home,2,15,7,8,40.96 8/26/2015 06:00 PM,Houston Dynamo,Away,1,14,11,3,59.04 8/28/2015 08:00 PM,San Jose Earthquakes,Home,1,10,3,7,50.27 8/28/2015 08:00 PM,Los Angeles Galaxy,Away,0,12,6,6,49.73 8/29/2015 01:00 PM,New York City,Home,1,17,3,14,55.88 8/29/2015 01:00 PM,Toronto FC,Home,2,16,5,11,43.19 8/29/2015 01:00 PM,Columbus Crew,Away,2,8,4,4,44.12 8/29/2015 01:00 PM,Montreal Impact,Away,1,21,9,12,56.81 8/29/2015 04:00 PM,Philadelphia Union,Home,0,21,4,17,59.37 8/29/2015 04:00 PM,New England Revolution,Away,1,24,5,19,40.63 8/29/2015 04:30 PM,Orlando City,Home,1,14,4,10,41.7 8/29/2015 04:30 PM,Chicago Fire,Away,1,16,10,6,58.3 8/29/2015 06:00 PM,Colorado Rapids,Home,2,16,6,10,59.12 8/29/2015 06:00 PM,FC Dallas,Home,2,11,2,9,43.8 8/29/2015 06:00 PM,Houston Dynamo,Home,2,12,8,4,51.2 8/29/2015 06:00 PM,Sporting Kansas City,Away,1,11,8,3,40.88 8/29/2015 06:00 PM,Real Salt Lake,Away,0,6,6,0,56.2 8/29/2015 06:00 PM,Vancouver Whitecaps,Away,0,18,5,13,48.8 8/30/2015 01:30 PM,Seattle Sounders,Home,2,9,4,5,58.76 8/30/2015 01:30 PM,Portland Timbers,Away,1,14,2,12,41.24 8/30/2015 04:00 PM,New York Red Bulls,Home,3,11,3,8,57.51 8/30/2015 04:00 PM,DC United,Away,0,15,12,3,42.49 9/5/2015 04:30 PM,New England Revolution,Home,3,19,10,9,42.54 9/5/2015 04:30 PM,Orlando City,Away,0,23,7,16,57.46 9/5/2015 05:00 PM,Montreal Impact,Home,4,15,10,5,54.28 9/5/2015 05:00 PM,Chicago Fire,Away,3,6,5,1,45.72 9/5/2015 07:00 PM,Seattle Sounders,Home,2,23,12,11,59.37 9/5/2015 07:00 PM,Toronto FC,Away,1,10,4,6,40.63 9/5/2015 07:30 PM,San Jose Earthquakes,Home,1,15,5,10,58.27 9/5/2015 07:30 PM,Philadelphia Union,Away,2,9,7,2,41.73 9/6/2015 04:00 PM,Columbus Crew,Home,0,10,4,6,57.08 9/6/2015 04:00 PM,FC Dallas,Away,3,11,9,2,42.92 9/9/2015 07:00 PM,Vancouver Whitecaps,Home,2,21,9,12,60.09 9/9/2015 07:00 PM,Colorado Rapids,Away,0,11,4,7,39.91 9/9/2015 07:30 PM,Portland Timbers,Home,0,7,3,4,47.36 9/9/2015 07:30 PM,Sporting Kansas City,Away,0,20,11,9,52.64 9/11/2015 04:00 PM,New York Red Bulls,Home,3,19,5,14,56.6 9/11/2015 04:00 PM,Chicago Fire,Away,2,16,6,10,43.4 9/12/2015 04:00 PM,Philadelphia Union,Home,1,17,5,12,47.62 9/12/2015 04:00 PM,Columbus Crew,Away,2,6,3,3,52.38 9/12/2015 05:30 PM,FC Dallas,Home,2,25,6,19,49.06 9/12/2015 05:30 PM,Houston Dynamo,Home,1,8,2,6,60.48 9/12/2015 05:30 PM,New York City,Away,1,20,7,13,50.94 9/12/2015 05:30 PM,Real Salt Lake,Away,3,12,8,4,39.52 9/12/2015 06:00 PM,Colorado Rapids,Home,1,10,10,0,57.05 9/12/2015 06:00 PM,DC United,Away,1,9,4,5,42.95 9/12/2015 07:30 PM,Los Angeles Galaxy,Home,0,6,5,1,59.39 9/12/2015 07:30 PM,San Jose Earthquakes,Home,1,15,10,5,47.61 9/12/2015 07:30 PM,Montreal Impact,Away,0,23,9,14,40.61 9/12/2015 07:30 PM,Seattle Sounders,Away,1,12,2,10,52.39 9/13/2015 02:00 PM,Toronto FC,Home,1,8,8,0,48.76 9/13/2015 02:00 PM,New England Revolution,Away,3,12,3,9,51.24 9/13/2015 04:00 PM,Orlando City,Home,3,16,2,14,53.64 9/13/2015 04:00 PM,Sporting Kansas City,Away,1,21,11,10,46.36 9/16/2015 04:30 PM,New England Revolution,Home,2,25,12,13,55.23 9/16/2015 04:30 PM,New York City,Home,2,10,2,8,46.21 9/16/2015 04:30 PM,New York Red Bulls,Away,1,13,7,6,44.77 9/16/2015 04:30 PM,Toronto FC,Away,0,13,11,2,53.79 9/16/2015 07:30 PM,San Jose Earthquakes,Home,1,7,3,4,41.21 9/16/2015 07:30 PM,Montreal Impact,Away,1,10,4,6,58.79 9/18/2015 04:00 PM,Sporting Kansas City,Home,3,12,8,4,60.2 9/18/2015 04:00 PM,FC Dallas,Away,1,15,8,7,39.8 9/19/2015 11:00 AM,Toronto FC,Home,3,7,6,1,49.62 9/19/2015 11:00 AM,Colorado Rapids,Away,1,10,7,3,50.38 9/19/2015 04:00 PM,DC United,Home,1,15,7,8,42.77 9/19/2015 04:00 PM,New York City,Home,3,18,11,7,58.79 9/19/2015 04:00 PM,Vancouver Whitecaps,Home,0,25,6,19,52.76 9/19/2015 04:00 PM,Columbus Crew,Away,2,15,10,5,57.23 9/19/2015 04:00 PM,San Jose Earthquakes,Away,2,23,3,20,41.21 9/19/2015 04:00 PM,Seattle Sounders,Away,3,20,12,8,47.24 9/19/2015 05:00 PM,Montreal Impact,Home,3,11,6,5,56.74 9/19/2015 05:00 PM,New England Revolution,Away,0,22,5,17,43.26 9/19/2015 05:30 PM,Chicago Fire,Home,0,11,7,4,54.02 9/19/2015 05:30 PM,Orlando City,Away,1,5,4,1,45.98 9/19/2015 06:30 PM,Real Salt Lake,Home,3,24,9,15,57.43 9/19/2015 06:30 PM,Los Angeles Galaxy,Away,0,15,8,7,42.57 9/20/2015 02:00 PM,Portland Timbers,Home,0,16,12,4,47.61 9/20/2015 02:00 PM,New York Red Bulls,Away,2,13,8,5,52.39 9/20/2015 04:00 PM,Philadelphia Union,Home,2,8,2,6,57.98 9/20/2015 04:00 PM,Houston Dynamo,Away,0,13,11,2,42.02 9/23/2015 05:00 PM,Montreal Impact,Home,2,10,6,4,50.35 9/23/2015 05:00 PM,Chicago Fire,Away,1,20,12,8,49.65 9/23/2015 05:30 PM,Houston Dynamo,Home,1,19,12,7,43.21 9/23/2015 05:30 PM,Sporting Kansas City,Away,0,6,5,1,56.79 9/25/2015 04:00 PM,New York Red Bulls,Home,2,17,12,5,51.54 9/25/2015 04:00 PM,Orlando City,Away,5,19,10,9,48.46 9/26/2015 11:00 AM,Toronto FC,Home,3,11,6,5,43.83 9/26/2015 11:00 AM,Chicago Fire,Away,2,11,8,3,56.17 9/26/2015 02:00 PM,Montreal Impact,Home,2,13,3,10,48.87 9/26/2015 02:00 PM,DC United,Away,0,15,11,4,51.13 9/26/2015 04:30 PM,Columbus Crew,Home,1,25,3,22,51.12 9/26/2015 04:30 PM,New England Revolution,Home,1,12,2,10,49.98 9/26/2015 04:30 PM,Portland Timbers,Away,2,14,12,2,48.88 9/26/2015 04:30 PM,Philadelphia Union,Away,1,25,10,15,50.02 9/26/2015 05:30 PM,Houston Dynamo,Home,3,14,3,11,59.45 9/26/2015 05:30 PM,Colorado Rapids,Away,2,11,10,1,40.55 9/26/2015 07:00 PM,Vancouver Whitecaps,Home,1,16,11,5,49.39 9/26/2015 07:00 PM,New York City,Away,2,9,5,4,50.61 9/27/2015 02:00 PM,Sporting Kansas City,Home,1,14,10,4,46.74 9/27/2015 02:00 PM,Seattle Sounders,Away,1,12,7,5,53.26 9/27/2015 04:00 PM,San Jose Earthquakes,Home,1,14,11,3,57.96 9/27/2015 04:00 PM,Real Salt Lake,Away,0,17,6,11,42.04 9/27/2015 06:30 PM,Los Angeles Galaxy,Home,3,23,3,20,49.63 9/27/2015 06:30 PM,FC Dallas,Away,2,10,3,7,50.37 10/2/2015 04:00 PM,DC United,Home,2,23,3,20,49.35 10/2/2015 04:00 PM,New York City,Away,1,5,5,0,50.65 10/3/2015 02:00 PM,Toronto FC,Home,3,23,7,16,60.62 10/3/2015 02:00 PM,Philadelphia Union,Away,1,22,4,18,39.38 10/3/2015 04:00 PM,New York Red Bulls,Home,2,12,11,1,59.09 10/3/2015 04:00 PM,Columbus Crew,Away,1,7,4,3,40.91 10/3/2015 04:30 PM,Orlando City,Home,2,14,7,7,40.42 10/3/2015 04:30 PM,Montreal Impact,Away,1,25,5,20,59.58 10/3/2015 05:30 PM,Chicago Fire,Home,3,19,5,14,49.07 10/3/2015 05:30 PM,New England Revolution,Away,1,11,3,8,50.93 10/3/2015 07:30 PM,Portland Timbers,Home,0,9,2,7,55.24 10/3/2015 07:30 PM,San Jose Earthquakes,Home,1,14,7,7,40.43 10/3/2015 07:30 PM,Sporting Kansas City,Away,1,11,7,4,44.76 10/3/2015 07:30 PM,Vancouver Whitecaps,Away,1,9,3,6,59.57 10/4/2015 02:00 PM,FC Dallas,Home,4,25,12,13,46.51 10/4/2015 02:00 PM,Houston Dynamo,Away,1,14,7,7,53.49 10/4/2015 04:00 PM,Colorado Rapids,Home,1,16,10,6,45.46 10/4/2015 04:00 PM,Real Salt Lake,Away,2,8,4,4,54.54 10/4/2015 06:30 PM,Seattle Sounders,Home,1,13,11,2,48.52 10/4/2015 06:30 PM,Los Angeles Galaxy,Away,1,7,2,5,51.48 10/7/2015 04:30 PM,New York Red Bulls,Home,2,25,11,14,52.03 10/7/2015 04:30 PM,Montreal Impact,Away,1,11,8,3,47.97 10/7/2015 07:00 PM,Vancouver Whitecaps,Home,0,15,10,5,48.16 10/7/2015 07:00 PM,FC Dallas,Away,0,12,6,6,51.84 10/10/2015 03:00 PM,Colorado Rapids,Home,0,23,12,11,60.24 10/10/2015 03:00 PM,Montreal Impact,Away,1,20,9,11,39.76 10/14/2015 04:00 PM,Toronto FC,Home,2,10,8,2,42.55 10/14/2015 04:00 PM,New York Red Bulls,Away,1,9,9,0,57.45 10/14/2015 06:00 PM,FC Dallas,Home,2,21,9,12,51.94 10/14/2015 06:00 PM,Vancouver Whitecaps,Away,0,23,7,16,48.06 10/14/2015 06:30 PM,Real Salt Lake,Home,0,16,6,10,52.07 10/14/2015 06:30 PM,Portland Timbers,Away,1,12,11,1,47.93 10/16/2015 04:00 PM,Orlando City,Home,2,22,11,11,60.66 10/16/2015 04:00 PM,New York City,Away,1,16,2,14,39.34 10/16/2015 08:00 PM,San Jose Earthquakes,Home,1,19,5,14,44.09 10/16/2015 08:00 PM,Sporting Kansas City,Away,0,15,12,3,55.91 10/17/2015 11:00 AM,Toronto FC,Home,0,22,4,18,45.27 10/17/2015 11:00 AM,Columbus Crew,Away,2,18,2,16,54.73 10/17/2015 04:30 PM,New England Revolution,Home,0,13,11,2,53.72 10/17/2015 04:30 PM,Montreal Impact,Away,1,18,12,6,46.28 10/17/2015 06:30 PM,Real Salt Lake,Home,0,24,5,19,56.97 10/17/2015 06:30 PM,FC Dallas,Away,1,16,4,12,43.03 10/18/2015 11:00 AM,DC United,Home,4,25,6,19,55.71 10/18/2015 11:00 AM,Chicago Fire,Away,0,21,6,15,44.29 10/18/2015 12:00 PM,New York Red Bulls,Home,4,21,5,16,56.4 10/18/2015 12:00 PM,Philadelphia Union,Away,1,15,9,6,43.6 10/18/2015 02:00 PM,Houston Dynamo,Home,1,20,5,15,47.15 10/18/2015 02:00 PM,Seattle Sounders,Away,1,14,6,8,52.85 10/18/2015 04:00 PM,Los Angeles Galaxy,Home,2,25,4,21,53.81 10/18/2015 04:00 PM,Portland Timbers,Away,5,17,11,6,46.19 10/21/2015 05:30 PM,Sporting Kansas City,Home,0,24,5,19,48.04 10/21/2015 05:30 PM,Colorado Rapids,Away,2,22,5,17,51.96 10/25/2015 02:00 PM,Columbus Crew,Home,5,8,5,3,54.89 10/25/2015 02:00 PM,Montreal Impact,Home,2,15,12,3,59.31 10/25/2015 02:00 PM,New York City,Home,1,11,3,8,50.04 10/25/2015 02:00 PM,Philadelphia Union,Home,1,20,6,14,57.29 10/25/2015 02:00 PM,DC United,Away,0,19,6,13,45.11 10/25/2015 02:00 PM,Toronto FC,Away,1,10,4,6,40.69 10/25/2015 02:00 PM,New England Revolution,Away,3,13,3,10,49.96 10/25/2015 02:00 PM,Orlando City,Away,0,19,9,10,42.71 10/25/2015 04:00 PM,Chicago Fire,Home,1,13,5,8,41.94 10/25/2015 04:00 PM,FC Dallas,Home,2,16,9,7,52.27 10/25/2015 04:00 PM,Portland Timbers,Home,4,13,3,10,49.89 10/25/2015 04:00 PM,Seattle Sounders,Home,3,23,4,19,40.81 10/25/2015 04:00 PM,Sporting Kansas City,Home,2,18,12,6,54.91 10/25/2015 04:00 PM,Vancouver Whitecaps,Home,3,12,9,3,43.31 10/25/2015 04:00 PM,New York Red Bulls,Away,2,15,7,8,58.06 10/25/2015 04:00 PM,San Jose Earthquakes,Away,1,21,10,11,47.73 10/25/2015 04:00 PM,Colorado Rapids,Away,1,22,12,10,50.11 10/25/2015 04:00 PM,Real Salt Lake,Away,1,13,8,5,59.19 10/25/2015 04:00 PM,Los Angeles Galaxy,Away,1,22,6,16,45.09 10/25/2015 04:00 PM,Houston Dynamo,Away,0,14,2,12,56.69 ,,,,,,, ,,,,,,,43.83
2 Answers
Severin Haselmann
24,517 PointsIn other countries,people use comma instead of period when it comes to decimals. I had to convert mine using CultureInfo.CreateSpecificCulture().
double possessionPercent;
if (double.TryParse(values[7], System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out possessionPercent))
{
gameResult.PossessionPercent = possessionPercent;
}
Aleksey Papushin
5,554 PointsI faced with the same problem and what helped to me is to use 4-argument double.TryParse version as:
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
double possessionPercent;
if (double.TryParse(values[7], NumberStyles.AllowDecimalPoint, culture, out possessionPercent))
{
gameResult.PossessionPercent = possessionPercent;
}
Linus Johansson
12,469 PointsLinus Johansson
12,469 Pointsyou can also put the System.Globalization at the top with the other namespaces like so --> using System.Globalization; then the if statement wont be as long (double.TryParse(values[7], NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out possessionPercent))