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 Integers

Calvin Secrest
Calvin Secrest
24,815 Points

I am confused on the syntax for this challenge

In the ParseWeatherForecast method in the Program.cs file, use int.TryParse to parse the Temperature property from the value of values[3] and add the parsed value to the weatherForecast object. Don't forget to declare the variable to hold the out parameter passed to the TryParse method.

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;
            }
            Condition condition;
            if (Enum.TryParse(values[2], out condition))
            {
                weatherForecast.Condition = condition;
            }
            var TryParse = new ParseWeatherForecast();
            if (int.TryParse(Temperature values[3], out weatherForecast)
            {
            }
            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 enum Condition
    {
        Rain,
        Cloudy,
        PartlyCloudy,
        PartlySunny,
        Sunny,
        Clear
    }
}

2 Answers

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;
            }
            Condition condition;
            if (Enum.TryParse(values[2], out condition))
            {
                weatherForecast.Condition = condition;
            }

            int temperature;
           if(int.TryParse(values[3], out temperature) )
            {
               weatherForecast.Temperature = temperature;
            }

            return weatherForecast;
        }
    }
}

I had some trouble with this challenge. This is the code I used that passed.

Roman Fincher
Roman Fincher
18,225 Points

What you have here: var TryParse = new ParseWeatherForecast(); if (int.TryParse(Temperature values[3], out weatherForecast) { } return weatherForecast;

is not working for several reasons. But first I'll try clarifying the goal:

You are trying to parse a string into an integer. The string is coming from the input to the ParseWeatherForecast function, which is a string array called "values".

values[3] holds the string you want to parse into an integer using TryParse()

This should mimic the provided lines above which parse the condition and timeOfDay, except you will be parsing the temperature instead.

Your first line creates a variable called TryParse and tries to instantiate an object of the ParseWeatherForecast class, which does not exist. You also do not want to call the ParseWeatherForecast function, because the function you are in will be calling itself. Instead, all we want to do is create an empty variable to hold the value of int.TryParse(values[3], ...) if it is able to parse values[3].

So, change your first line to simply:

int temperature;

then call:

if(int.TryParse(values[3], out temperature) )
{
     //do something
}

The purpose of this is that we initialized temperature as a variable that the result of int.TryParse() can be assigned to if it works. If it doesn't work, TryParse returns false and the // do something part of the if block won't be run. If it does work, we have an integer value for temperature.

At that point, all we want to do is assign the int value of temperature to the property weatherForecast.Temperature (i.e.

weatherForcast.Temperature = temperature;

... Hint: replace //do something with that assignment.)

That's a little more of giving you the answer than I'd like to do, but I hope the explanation clarifies what our actual objective and approach should be as well as why. I know this "out" part is a little confusing at this stage. The reason we need "out" and the if block is to avoid an exception. We can't assign a string to an int if the int cannot be parsed. If TryParse fails, that line won't be run. If it succeeds, we need a variable to store the parsed result, and that's what the "out temperature" is for.

Calvin Secrest
Calvin Secrest
24,815 Points

Thank you for the assistance