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# Basics (Retired) Perfect Final

Sean Flanagan
Sean Flanagan
33,235 Points

C# Basics Final Task 2

Hi. I'm stuck on this one.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0;
            String times = Console.ReadLine();
            int totals = int.Parse(times);
            while (counter < totals)
            {
              Console.Write("\"Yay!\"! ");
              counter += 1;
            }
            try
            {

            }
            catch
            {

            }
        }
    }
}

I've tried to throw a Format Exception.

I've also noticed that despite ticking the "Attach my code to this post" tickbox, other users still ask to see my syntax, so I wonder if there's a technical fault here. I'd appreciate any help.

2 Answers

Stéphane Diez
Stéphane Diez
19,350 Points

It should look something like this:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

                Console.Write("Enter the number of times to print \"Yay!\": ");

                var entry = Console.ReadLine();
                try
                {

                    var NumberOfTimes = int.Parse(entry);

                   while(NumberOfTimes>0)
                   {
                        Console.WriteLine("Yay!");
                        NumberOfTimes--;
                   }

                }

                catch (FormatException)//ArgumentNullException
                {
                    Console.WriteLine("You must enter a whole number.");
                }
        }
    }
}

You want the try to be before you make the parsing. Then the while must be inside the try aswell. In the catch you want to put FormatException inside the (). You can check the c# documentation if you need some more input, or try to review the actual video again.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Stephane. It's worked. I've given you an up vote and Best Answer. Thanks. :)