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

D Elis
D Elis
9,356 Points

Could use some help with C# Streams-Working with floating point numbers

I am stuck on this question

Program.cs
using System;
using System.IO;

namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public static void Main(string[] arg)
        {
        }

       public static WeatherForecast ParseWeatherForecast(string[] values)
       {
            var weatherForecast = new WeatherForecast();
            weatherForecast.WeatherStationId = values[0];
            DateTime timeOfDay;
            if (DateTime.TryParse(values[1], out timeOfDay))
            {
                weatherForecast.TimeOfDay = timeOfDay;
            }
            int temperature;
            if (int.TryParse(values[3], out temperature))
            {
                weatherForecast.Temperature = temperature;
            }
           int v1;
           if(double.TryParse(values[4], out v1)){
               weatherForecast.v1=v1;
           }
           int v2;
           if(double.TryParse(values[5], out v2)){
               weatherForecast.v2=v2;
           }

           return weatherForecast;





        }
    }
}
WeatherForecast.cs
using System;

/* Sample CSV Data 

weather_station_id,time_of_day,condition,temperature,precipitation_chance,precipitation_amount
HGKL8Q,06/11/2016 0:00,Rain,53,0.3,0.03
HGKL8Q,06/11/2016 6:00,Cloudy,56,0.08,0.01
HGKL8Q,06/11/2016 12:00,PartlyCloudy,70,0,0
HGKL8Q,06/11/2016 18:00,Sunny,76,0,0
HGKL8Q,06/11/2016 19:00,Clear,74,0,0
*/

namespace Treehouse.CodeChallenges
{
    public class WeatherForecast
    {
        public string WeatherStationId { get; set; }
        public DateTime TimeOfDay { get; set; }
        public Condition Condition { get; set; }
        public int Temperature { get; set; }
        public double PrecipitationChance { get; set; }
        public double PrecipitationAmount { get; set; }
    }

    public enum Condition
    {
        Rain,
        Cloudy,
        PartlyCloudy,
        PartlySunny,
        Sunny,
        Clear
    }
}

2 Answers

// WeatherForecast.cs

using System;

/* Sample CSV Data 

weather_station_id,time_of_day,condition,temperature,precipitation_chance,precipitation_amount
HGKL8Q,06/11/2016 0:00,Rain,53,0.3,0.03
HGKL8Q,06/11/2016 6:00,Cloudy,56,0.08,0.01
HGKL8Q,06/11/2016 12:00,PartlyCloudy,70,0,0
HGKL8Q,06/11/2016 18:00,Sunny,76,0,0
HGKL8Q,06/11/2016 19:00,Clear,74,0,0
*/

namespace Treehouse.CodeChallenges
{
    public class WeatherForecast
    {
        public string WeatherStationId { get; set; }
        public DateTime TimeOfDay { get; set; }
        public Condition Condition { get; set; }
        public int Temperature { get; set; }
        public double PrecipitationChance { get; set; }
        public double PrecipitationAmount { get; set; }
    }

    public enum Condition
    {
        Rain,
        Cloudy,
        PartlyCloudy,
        PartlySunny,
        Sunny,
        Clear
    }
}
//Program.cs

using System;
using System.IO;

namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public static void Main(string[] arg)
        {
        }

       public static WeatherForecast ParseWeatherForecast(string[] values)
       {
            var weatherForecast = new WeatherForecast();
            weatherForecast.WeatherStationId = values[0];
            DateTime timeOfDay;
            if (DateTime.TryParse(values[1], out timeOfDay))
            {
                weatherForecast.TimeOfDay = timeOfDay;
            }
            int temperature;
            if (int.TryParse(values[3], out temperature))
            {
                weatherForecast.Temperature = temperature;
            }

           // create a variable- I created someVariable
           double someVariable;
           // lets get the get the values out and assign them to someVariable
           if (double.TryParse(values[4], out someVariable))
           {
               weatherForecast.PrecipitationChance = someVariable;                                  
           }
           // create a variable- I created someOtherVariable
           double someOtherVariable;
           // lets get the get the values out and assign them to someOtherVariable
           if (double.TryParse(values[5], out someOtherVariable))
           {
               weatherForecast.PrecipitationAmount = someOtherVariable;                                  
           }

            return weatherForecast;
        }
    }
}
D Elis
D Elis
9,356 Points

thanks!

D Elis
D Elis
9,356 Points

Challenge Task 2 of 2 In the ParseWeatherForecast method, use double.TryParse to parse the PrecipitationChance and PrecipitationAmount properties from the value of values[4] and values[5] respectively, and add the parsed values to the weatherForecast object. Don't forget to declare the variable to hold the out parameter to pass to the TryParse method.

I had a lot of problems with this also. Please see below for what I came up with that worked.