1

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);
        }
1
  • Your code works "as-is", I copied and pasted -- check this .NET Fiddle dotnetfiddle.net/DrzNMb Commented Aug 17, 2016 at 14:48

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, adding console.write with your code works wonderfully ^^
0

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);
    }

Comments

0

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.

Comments

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.