1

Im creating a program that will only greet the person if named carley and zack.

string name1 = "Carley";
        string name2 = "Zack";

        Console.Write("What's your name? : ");
        string name =  Console.ReadLine();


        if (name == name1)
        {
            Console.WriteLine("Glad to meet you " + name + "!");
        }
        else if (name == name2)
        {
            Console.WriteLine("Glad to meet you " + name + "!");
        }
        else
        {
            Console.WriteLine("press any key to exit.");
        }
        Console.Read();

Here I am, hoping to simplify the code by trying to put the evaluation of the input in one statement for the "if":

        if (name == name1 && name2)
        {
            Console.WriteLine("Glad to meet you " + name + "!");
        }
        else
        {
            Console.WriteLine("press any key to exit.");
        }
        Console.Read();

But it says the AND (&&) cant be applied for boolean and string. Can you please explain and help me out with this code, and also figuring out, it can accept input regardless if it has upper and lowercase etc.

1

3 Answers 3

5

You can't use && to compare one value against two other values. You need two comparisons:

if (name == name1 && name == name2)

Also, && doesn't match the logic in your original code. || would be the equivalent of your original code:

if (name == name1 || name == name2)

and also figuring out, it can accept input regardless if it has upper and lowercase etc.

You can use name.Equals(..., StringComparison.InvariantCultureIgnoreCase) for a case insensitive comparison:

if (name.Equals(name1, StringComparison.InvariantCultureIgnoreCase) || 
    name.Equals(name2, StringComparison.InvariantCultureIgnoreCase))

Since this is starting to get a bit verbose, you can put the acceptable names into an array and then use .Any(). This approach will allow you to specify any number of valid names:

string[] validNames = new[] { "Carley", "Zack" };
if (validNames.Any(n => 
       n.Equals(name, StringComparison.InvariantCultureIgnoreCase)))
{
    // ...
}

But given that this approach is a bit advanced for your level, you may want to stick to the second-to-last snippet for now.

Sign up to request clarification or add additional context in comments.

2 Comments

name cannot be equal to name1 AND equel to name2, did you mean OR?
Awesome! thanks for giving me a wide range of option and explanation here. I'll keep this in mind for future use :)
5

Actually, it should be

    if (name == name1 || name == name2)
    {
        Console.WriteLine("Glad to meet you " + name + "!");
    }
    else
    {
        Console.WriteLine("press any key to exit.");
    }

When using ||, the entire condition will evaluate to true if any of the conditions in each side of the || is true.

When using &&, the entire condition will evaluate to true only if all conditions in either side of the && are true.

1 Comment

Haha, funny for me not to think that it should be evaluating 2 statements, not between a statement then a variable. I'll keep this in mind. Thanks mate!
2

Both sides of the && will be evaluated (as a boolean). name2 only doesn't do what you want. You should explicit specify the comparison.

Also you want to use the OR: ||

if ((name == name1) || (name == name2))

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.