Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell my console app not to continue until i enter a number?

Tags:

c#

.net

console

static int beverageSelection()
{
    Console.WriteLine();

    int brand;

    string _val = "";
    Console.Write("Enter number: ");
    ConsoleKeyInfo key;
    do
    {
        key = Console.ReadKey(true);
        if (key.Key != ConsoleKey.Backspace)
        {
            double val = 0;
            bool _x = double.TryParse(key.KeyChar.ToString(), out val);
            if (_x)
            {
                _val += key.KeyChar;
                Console.Write(key.KeyChar);
            }
        }
        else
        {
            if (key.Key == ConsoleKey.Backspace && _val.Length > 0)
            {
                _val = _val.Substring(0, (_val.Length - 1));
                Console.Write("\b \b");
            }
        }
    }
    while (key.Key != ConsoleKey.Enter);

    brand = Convert.ToInt32(Console.ReadLine());

    return brand;
}

The method above is giving me a headache. I cannot figure out how to tell the console app that it shouldn't let me input any character or even the enter button until I have typed a number into the console. Then and only then should I be able to press enter. In any case this program is a vending machine I created for fun and I do not fully understand the do while loop in this code just to be clear.

like image 827
Paludis Avatar asked Dec 05 '25 09:12

Paludis


2 Answers

Use Console.ReadLine instead of Console.ReadKey:

int brand = 0;
while (true)
{
    string val = Console.ReadLine();
    if (int.TryParse(val, out brand)) break;
}
like image 191
Ashkan Mobayen Khiabani Avatar answered Dec 07 '25 23:12

Ashkan Mobayen Khiabani


Improved your original code to display and accept only numbers

    static void Main(string[] args)
    {
        Console.WriteLine();

        int brand;

        string _val = "";
        Console.Write("Enter number: ");
        while(true)
        {
            var key = Console.ReadKey(true);
            if (key.Key == ConsoleKey.Enter && int.TryParse(_val, out brand))
            {
                Console.WriteLine();
                break;
            }
            if (key.Key != ConsoleKey.Backspace)
            {
                int val;
                if (int.TryParse(key.KeyChar.ToString(), out val))
                {
                    _val += key.KeyChar;
                    Console.Write(key.KeyChar);
                }
            }
            else
            {
                if (_val.Length > 0)
                {
                    _val = _val.Substring(0, _val.Length - 1);
                    Console.Write("\b \b");
                }
            }
        }

        Console.WriteLine("Brand: {0}", brand);
        Console.ReadKey();
    }
like image 32
Mitya Avatar answered Dec 07 '25 22:12

Mitya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!