Home > OS >  C# - Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’
C# - Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

Time:07-21

I know there is a question similar to this one but the naswers there didnt help me that much it was compicated so i asked this new one

i was trying to convert char into upper case by .ToUpper method

but it is give me this issue

Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

here is the code

char UserChoice = Convert.ToChar(Console.ReadLine());
char upperCaseChoice = Char.ToUpperInvariant(UserChoice);

if (UserChoice == "Y" || upperCaseChoice == "YES")
{
    TotalCoffeeCost = askForCoffee(TotalCoffeeCost); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
    invalidChoice = false;
}
else
{
    Console.WriteLine("");
    Console.WriteLine(" Please enter a valid choice");
}

CodePudding user response:

You are setting the type of the variable to a character, set it to a string.

string UserChoice = Console.ReadLine();

if (UserChoice == "y")
{
    Console.WriteLine("test");
}

CodePudding user response:

Why don't you use the following method?

    // original string
    string UserChoice = Console.ReadLine();

    // string converted to Upper case
    string upperCaseChoice = UserChoice.ToUpper();



    if (UserChoice == "Y" || upperCaseChoice == "YES")
    {
        //Do Somthing

    }
    else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
    {
        //Do Somthing
    }
    else
    {
        Console.WriteLine("");
        Console.WriteLine(" Please enter a valid choice");
    }

In the first step you cannot convert the string to a character because Character refers to a single character value You must convert the value to an array of characters

    var userChoice = Console.ReadLine().ToCharArray();

    var userChoiceFirstChar = Convert.ToString(userChoice.First()).ToUpperInvariant();
    var userChoiceJoin = string.Join("", userChoice).ToUpper();

    
    if (userChoiceFirstChar == "Y" || userChoiceJoin == "YES")
    {
    }
    else if (userChoiceFirstChar == "N" || userChoiceJoin == "NO")
    {
    }
    else
    {
        Console.WriteLine("");
        Console.WriteLine(" Please enter a valid choice");
    }

CodePudding user response:

Well, string is a collection of characters and that's why the fragment below doesn't compile:

char upperCaseChoice = ... // single character

...
// how come can compiler compare single character with a collection?

if ( ... upperCaseChoice == "YES" ...)

I suggest to extract method, let it be ReadBool which takes the title (the question we'll put for users) and returns bool (true or false) user's answer:

private static readonly IReadOnlyDictionary<string, bool> s_Bools = 
  new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase) {
    //TODO: add more synonyms, like `{"true", true}`, `{"OK", true}` etc.
    {"y",   true},
    {"yes", true},
    {"n",   false},
    {"no",  false}, 
};

private static bool ReadBool(string title) {
  // keep asking user until known response is provided
  while (true) {
    if (!string.IsNullOrWhiteSpace(title)) 
      Console.WriteLine(title);

    // if we know the response for user's input, return it
    // .Trim() - let's be nice and tolerate leading and trailing spaces
    if (s_Bools.TryGetValue(Console.ReadLine().Trim(), out bool result))
      return result;

    Console.WriteLine("");
    Console.WriteLine(" Please enter a valid choice"); 
  }
}

Then you can use it as simple as

if (ReadBool("Please, make your choice (yes/no)")) {
  //TODO: Relevant code for "Yes"
}
else {
  //TODO: Relevant code for "No"
}
  • Related