sylvann sylvann - 9 days ago 4
C# Question

How to check if string not equals a Boolean

Hi guys I want to check if a User input(String) not equals to a Boolean but apparently can't do that:

For example:

using System;
using System.Linq;

namespace SymbolCheck
{
class Program
{
static void Main(string[] args)
{
String symbol = Console.ReadLine();
int value;
bool isVowel = "aeiouAEIOU".IndexOf(symbol) >= 0;

if (int.TryParse(symbol, out value))
{
Console.WriteLine("Input is a number: " + symbol);
}

else if (symbol.Equals(isVowel) & symbol.Any(char.IsUpper))
{
Console.WriteLine("Input is a uppercase vowel: "+ symbol);
}

else if (symbol != isVowel)
{
Console.WriteLine("Input is a consonant: ", symbol);
}


....

I got a way to get around it but want to know if there is another way of doing it:

else if (symbol.Equals(isVowel) & symbol.Any(char.IsLower))
{
Console.WriteLine("Input is a lowercase vowel: ", symbol);
}

else if (!int.TryParse(symbol, out value) & symbol.Any(char.IsUpper))
{
Console.WriteLine("Input is a uppercase consonant: " + symbol);
}


...

Answer

You probably want to check if both conditions are true: So use the && symbol. Furtherfore your isVowel is a boolean, so use that directly (symbol.Equals(isVowel.ToString() does not make any sense)

if (isVowel) && symbol.Any(char.IsUpper))

The other check would be

if (!isVowel)