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# Basic Code Challenge: Final 1/2

Please I need some help on this challenge.

Challenge Task 1 of 2 Add input validation to the below program by printing "You must enter a whole number." if the user enters a decimal or something that isn’t a number. Wrap all of the code contained in the Main method in a try/catch block The catch block should catch FormatException exceptions Inside of the catch block, output to the console the message "You must enter a whole number."

No errors when it was compiled but getting a bummer,

Bummer: Did you catch FormatException thrown by int.Parse using a try/catch block?

using System;
namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();

            int count = int.Parse(input);

            int i = 0;
            while(i < count)
            {
                try
                {
                    i += 1;  
                }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }

                Console.WriteLine("Yay!");
            }
        }
    }
}

2 Answers

Allan Clark
Allan Clark
10,810 Points

The int.Parse call is what you are try/catch-ing. So you should just need to move that line into the try block.

try
{
       int count = int.Parse(input);
       i += 1;  
}

Thanks Allan, unfortunately I get an error since the variable count is move inside the while statement. Please see the error and the updated code below.

Program.cs(11,23): error CS0103: The name `count' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();

            int i = 0;
            while(i < count)
            {
                try
                {
                    int count = int.Parse(input);
                    i += 1;
                }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
                Console.WriteLine("Yay!");
            }
        }
    }
}

Ok. I got the answer. Below is the code:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                string input = Console.ReadLine();

                int count = int.Parse(input);

                int i = 0;
                while(i < count)
                {
                    i += 1;   
                    Console.WriteLine("Yay!");
                }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}