Trying to compare chars in C#

51,105

Solution 1

Right now, the conditional in your while statement will always be true. A good way to test for this is to put a breakpoint where the while loop is being tested, and then "Add watch" to each part of the conditional.

Change

while ((input[0] != 'N') || (input[0] != 'Y'))

to

while ((input[0] != 'N') && (input[0] != 'Y'))

Solution 2

As everyone has already pointed out, you are using || where you should be using &&.

Beyond that, you are also attempting to access the first character of a string that is possible empty. This will cause an exception if the user just hits 'Enter'.

Since you asked for a better way, here's one alternative, that uses Console.ReadKey instead of Console.ReadLine, as you only seem to be interested in getting a character anyway. It also has the advantage that it is not case-sensitive.

while (true)
{
    ConsoleKeyInfo key = Console.ReadKey();
    Console.WriteLine(""); // Just for nice typesetting.

    if (key.Key == ConsoleKey.N)
    {
        Console.WriteLine("NO");
        break;
    }

    if (key.Key == ConsoleKey.Y)
    {
        Console.WriteLine("YES");
        break;
    }
}

Solution 3

The problem is in condition checking.

while ((input[0] != 'N') || (input[0] != 'Y'))

Suppose 'N' is entered. Now condition (input[0] != 'Y') becomes false and it should break out of while loop but because of || with (input[0] != 'N') which remains true, the end result in condition comes out to be true and hence it never breaks out of loop.

Replace || with &&

Solution 4

Try this:-

String input = Console.ReadLine();
        while ((input[0] != 'N') && (input[0] != 'Y'))
        {
            input = Console.ReadLine();

        }
        if (input[0] == 'N')
        {
            Console.WriteLine("NO");
            Console.ReadKey();
        }
        else if (input[0] == 'Y')
        {
            Console.WriteLine("YES");
            Console.ReadKey();
        }
Share:
51,105
JahKnows
Author by

JahKnows

I am a Data Engineering Student currently working in industry.

Updated on June 15, 2020

Comments

  • JahKnows
    JahKnows almost 4 years

    I am new to C# I have started learning it to broaden the programming languages to my disposal but I have run into a little problem I did not encounter in neither C nor Java.

    I am trying to get a user response from the keyboard and then comparing it with probable cases and if none of those cases matched up then I want the user to have to repeat the process until he has entered in a correct value.

    String input = Console.ReadLine();
    
    while ((input[0] != 'N') || (input[0] != 'Y'))
        input = Console.ReadLine();       
    if (input[0] == 'N')
    {
        Console.WriteLine("NO");
        Console.ReadKey();
    }
    else if (input[0] == 'Y')
    {
        Console.WriteLine("YES");
        Console.ReadKey();
    } 
    

    This is by far not the most efficient way I have tried, i also tried doing a do while loop and many other variants.

    The problem I encounter is that when the while loop is not activated everything works just fine, but when I add it in, it always enters the loop even if the input is N or Y and can never leave the loop even though it is clear that it is wrong.

    Please if someone can give me some insight as to why this is occurring or if someone may propose a better way of doing this it would be greatly appreciated. Thank you.

    Karim