Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get the reverse method of the following method ....

In the following code: if any input other than integar is placed then an error message is given and only the integar value greater than 20 is made as valid input....

I want to get the method which shows an error message if any integar value is given as input and only the string datatype would be valid ... how to get the reverse of this???...REGARDS

What I have tried:

C#
using System;

namespace C____SPELLiNG_CHECKER
{

    

    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 InputValidator() { }

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

            IsValidInput = IsNumber;
        }

        
        public 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
            
            if (!result.IsValidInput)
            {
                result = new InputValidator(Console.ReadLine());

                if (!result.IsValidInput)
                {
                    result.ShowErrorMessage();
                }
                else
                {
                    Console.WriteLine("no error found");
                }

            }
            return  result.Litres;
        }
        
        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;
            }
           
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            InputValidator I = new InputValidator();
            I.InputFuel();  
            I.ShowErrorMessage();

           Console.ReadLine();
            Console.ReadKey();
        }
    }
}
Posted
Updated 22-Mar-21 6:04am
Comments
Maciej Los 22-Mar-21 10:01am    
If you want to compare to integer, why are you trying to parse it into double?
Richard MacCutchan 22-Mar-21 11:25am    
Your code is a bit confusing. In your InputFuel method you create a new instance of your class. All you need there is a simple console message to ask for the fuel, and a check to see that it is a valid number not greater than 20. If it is not valid then post the error message and ask the user to try again. You probably also need to add a retry count to stop it going on forever.

Not sure what you want exactly, but maybe this will be of help:
string text = "12.34";
int retNum;

// IsNumeric() equivalent
if (int.TryParse(
    text,
    System.Globalization.NumberStyles.Integer,
    System.Globalization.NumberFormatInfo.InvariantInfo,
    out retNum))
{
    // Do something;
    Console.WriteLine("Integer " + retNum);
}
else
{
    Console.WriteLine("Error: not an integer: " + text);
}
 
Share this answer
 
v2
Comments
R i Emon 22-Mar-21 10:47am    
Simply want to show an error message if I give any integar value as a user input...Thats it...
RickZeeland 22-Mar-21 13:00pm    
Updated the solution with an example that shows how you can use it :)
This is really all you need:
C#
class InputValidator
{
    public bool IsValidInput { get; private set; }
    public double Litres { get; private set; }

    public InputValidator()
    {
        IsValidInput = false;
    }

    public void InputFuel()
    {
        Litres = 0.0;
        int retry = 0;
        while (retry < 3)
        {
            retry++;
            Console.Write("Enter amount of fuel used in litres : ");
            string text = Console.ReadLine();
            double litres;
            if (double.TryParse(text, out litres))
            {
                IsValidInput = litres <= 20;
                if (IsValidInput)
                {
                    Litres = litres;
                    break;
                }
            }
            if (retry == 3)
            {
                Console.WriteLine($"{retry} attempts. Program terminating");
            }
            else if (!IsValidInput)
            {
                Console.WriteLine($"{text} is not valid, please try again");
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        InputValidator I = new InputValidator();
        I.InputFuel();
        if (I.IsValidInput)
        {
            Console.WriteLine($"{I.Litres} litres of fuel input.");
        }
        else
        {
            Console.WriteLine("No valid input received.");
        }
       Console.ReadLine();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900