简体   繁体   中英

How do I check if a variable is equal to a string or an int?

For example, lets say I have and int called input. And then I get input for it. Like this:

input = Console.ReadLine();

But when the user gets prompted, input is a string, and then it gets an error and crashes the program. How do I check if a string was entered for input?

Is there an option for it like if (input.equals(string)) or something like that?

EDIT: I tried doing the TryParse but I think I'm doing it wrong. I posted the code below

    string numberOne;
    string numberTwo;
    double answer;
    string operand;

    Console.WriteLine("Enter the first number");
    numberOne = Console.ReadLine();
    if (double.TryParse(numberOne, out double value))
    {
        Convert.ToInt32(numberOne);
    }
    else
    {
        Console.WriteLine("Non integer entered. Please enter a valid number.");
    }

    Console.WriteLine("Enter the operator");
    operand = Console.ReadLine();

    Console.WriteLine("Enter the second number");
    numberTwo = Console.ReadLine();
    if (double.TryParse(numberTwo, out double value2))
    {
        Convert.ToInt32(numberTwo);
    }
    else
    {
        Console.WriteLine("Non integer entered. Please enter a valid number.");
    }




    switch(operand)
    {
        case "+":
            answer = numberOne + numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "-":
            answer = numberOne - numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "*":
            answer = numberOne * numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "/":
            answer = numberOne / numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        default:
            Console.WriteLine("Please enter a valid operator such as +, -, *, or /");
            break;



    }

Console.Readline() always returns a string, however you can do this to check if an integer is inputed and continue asking for correct type of input until user delivers it.

string input = Console.ReadLine();
int parsed;
while (!int.TryParse(input, out parsed))
{
     Console.WriteLine("Please enter a number!");
     input = Console.ReadLine();
}

//do something with parsed

Console.ReadLine always returns a string . It's doesn't return differently typed things based the content read from console. It's your job to decide if the value entered matches your requirements (in this case it should be something that can be parsed to an int).

You can use int.TryParse to try parsing a string as int without it throwing an exception when parsing fails.

string input = Console.ReadLine();
if(int.TryParse(input, out int value))
{
    // value contains an int parsed out of input
}
else
{
    // parsing fails, you can do something about that, e.g. ask user for different input
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM