0

I've literally just started C#... Today and I'm attempting to write a very simple application. However, when I use the console.readkey if anything other than a number is entered, it immediately crashes.

I completely understand that its attempting to parse out the numbers and convert them to a string however though, How do I keep the application from crashing or failing if someone doesn't insert a number. I've googled around but apparently this is a pretty specific thing.

All I'm trying to do is nullify or Parse out anything that's not numbers so the application doesn't get confused and shut down.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
        Console.Write("what is your age?");
        string agestring = Console.ReadLine();
        int age = Int32.Parse(agestring);

        if (age >= 21)
        {
            Console.WriteLine("congrats, you're you can get drunk!");

        }
        else if (age < 21)
        {
            Console.WriteLine("Sorrrrrryyyyyyy =(");
        }
        else
        {
            Console.WriteLine("Sorry Thats not a valid input");
        }
    }
}
2
  • +1 for typing that much ! Commented Apr 20, 2014 at 19:46
  • Look up Exceptions. Without understanding how exceptions work, you won't get far. Commented Apr 20, 2014 at 19:52

2 Answers 2

3

Try this one:

int age;

if(Int32.TryParse(agestring, out age))
{
    if (age >= 21)
    {
        Console.WriteLine("congrats, you're you can get drunk!");
    }
    else
    {
        Console.WriteLine("Sorrrrrryyyyyyy =(");
    }   
}
else
{
    Console.WriteLine("Sorry Thats not a valid input");
}

Using the Int32.TryParse method you can check, if the parsing of the input is successful. If it is not, then you print a message to the console.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Int32.TryParse method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
        Console.Write("what is your age?");
        string agestring = Console.ReadLine();
        int age;

        if (Int32.TryParse(agestring, out age))
        {
            if (age >= 21)
            {
                Console.WriteLine("congrats, you're you can get drunk!");

            }
            else if (age < 21)
            {
                Console.WriteLine("Sorrrrrryyyyyyy =(");
            }


        }
        else
        {
            Console.WriteLine("Sorry Thats not a valid input");
        }


    }
}

}

3 Comments

I tried to use TryParse, however when I do it just throws a "No Overload for method" TryParse and says I'm using it incorrectly =/
You need to pass TryParse an out parameter, unlike Parse. msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
Ah it works! Thank you, I wasn't sure if you could use an if statement within an if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.