lufee
lufee

Reputation: 169

C# error message displayed based on text or number input

I'm trying to write line where if text (anything other than number) typed it shows error message that it is not number and ask user to type again.

Also I have implanted that number must be 20 or higher which works fine and when ever user input less than 20 it shows error message and ask user again..

so my problem is that it shows error message for everything including number less than 20 and text.

So how can I make else statement that shows different message if text is type rather than number?

static double InputFuel() {
    double fFuel;
    string text;
    bool badValue = true;

    Console.Write("Enter amount of fuel used in litres : ");
    //Check if fule entered is greater than 20, if not ask again
    do {

        text = Console.ReadLine();
        if (double.TryParse(text, out fFuel) && fFuel >= 20) {
            badValue = false;
        }

        else {
            Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text);
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    } while (badValue);
    return fFuel;
}//end InputFuel

I tried something like this but not working

        else (!int.TryParse(text, out num) {
            Console.WriteLine("\n\t {0} is not a number  \n\n", text);
        }

Upvotes: 1

Views: 3057

Answers (3)

Fabjan
Fabjan

Reputation: 13676

If you're looking for 'more advanced' or 'more enterprise-like' way of validating user input you can create a struct or class that will do the validation and provide some information about it :

class InputValidator
{
    public string Input { get; set; }
    public bool IsValidInput { get; set; }
    public bool IsAboveThreshHold { get; set; }
    public bool IsNumber { get; set; }
    public double Litres { get; set; }

    public ValidationResult() { }

    public ValidationResult(string text)
    {
        double litres; Input = text;
        if (double.TryParse(text, out litres))
        {
            Litres = litres;
            IsAboveThreshHold = litres > 20;
            IsNumber = true;                
        }

        IsValidInput = IsNumber && IsAboveThreshHold;
    }

    public void ShowErrorMessage()
    {
        if (!IsNumber)
        {
            Console.WriteLine($"\n\t {Input} is not a valid number \n\n");
            Console.Write("Please re-enter a number greater than 20 : ");                
            return;
        }
        if(!IsAboveThreshHold)
        {
            Console.WriteLine($"\n\t {Input} is below the minimum value of 20  \n\n");
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    }
}

And use this class very easily :

    static double InputFuel()
    {
        var result = new InputValidator();

        Console.Write("Enter amount of fuel used in litres : ");

        //Check if fule entered is greater than 20, if not ask again
        while (!result.IsValidInput)
        {
            result = new InputValidator(Console.ReadLine());

            if (!result.IsValidInput) result.ShowErrorMessage();                    
        }
        return result.Litres;
    }

P.S. While in simple cases this would be an overkill but in more complex cases that are common in enterprise projects this approach is much better to use.

Upvotes: 0

Krzyserious
Krzyserious

Reputation: 364

Firstly I would check if input is number and after that if number is greater than 20

static double InputFuel() {
    double fFuel;
    string text;
    bool badValue = true;

    Console.Write("Enter amount of fuel used in litres : ");
    //Check if fule entered is greater than 20, if not ask again
    do {

        text = Console.ReadLine();
    if (!double.TryParse(text, out fFuel) {
            Console.WriteLine("\n\t {0} is not a number  \n\n", text);
        }

        else if (fFuel >= 20) {
            badValue = false;
        }

        else {
            Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text);
            Console.Write("Please re-enter a number greater than 20 : ");
        }
    } while (badValue);
    return fFuel;
}//end InputFuel

Upvotes: 3

Rahul
Rahul

Reputation: 77866

You can your modify your current condition to be nested like below

    if (double.TryParse(text, out fFuel)) {
      if(fFuel >= 20) badValue = false;
      else {
        Console.WriteLine("\n\t {0} is below the minimum value of 20  \n\n", text); 
        Console.Write("Please re-enter a number greater than 20 : ");
      }
    }
    else {
      Console.WriteLine("\n\t {0} is not a number  \n\n", text);
    }

Upvotes: 0

Related Questions