Jump to content

[C#] How do I use logical operators on strings acting on numbers?

dialgarocksful
Go to solution Solved by Sauron,

 

I have that prior to the condition. the if condition is inside a for loop

for (int x = 0; x <= name.Length; x++)

 

Yeah, I had imagined that and that part seems correct. If you use .Length and 1 instead of '1' you should be set.

if (!((name[x] >= 'A' && name[x] <= 'Z') || (name[x] >= 'a' && name[x] <= 'z') || (name[x] == ' ') || (name >= '1'))) 

I have this condition. It does work without name >= '1', but when I put it back, it gives me this:

 

CS0019: Operator '>=' cannot be applied to operands of type 'string' and 'char'

 

 

name is type string, used on an array, to check character per character content

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

So you are comparing the string's length to be greater or equal to one, not an empty string?

Link to comment
Share on other sites

Link to post
Share on other sites

if (!((name[x] >= 'A' && name[x] <= 'Z') || (name[x] >= 'a' && name[x] <= 'z') || (name[x] == ' ') || (name >= '1'))) 

I have this condition. It does work without name >= '1', but when I put it back, it gives me this:

 

CS0019: Operator '>=' cannot be applied to operands of type 'string' and 'char'

 

 

name is type string, used on an array, to check character per character content

 

https://msdn.microsoft.com/en-us/library/system.string.length(v=vs.110).aspx

 

Edit: Here's some example code

namespace Test{    class Program    {        static void Main(string[] args)        {            string x = "bananas";            Console.WriteLine(x.Length);            if (x.Length > 5)            {                // do stuff if length of string is greater than 5                Console.WriteLine("The string has a length greater than 5.");            }        }    }}

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is you are using apostrophes instead of quotation marks.

 

'a' denotes a char

"a" denotes a string

 

Check out string.Compare() for the rest of your method :)

 

Also you can only apply >= or <= operators to numbers you can't compare strings using those operators

 

https://msdn.microsoft.com/en-us/library/hx063734.aspx

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
Share on other sites

Link to post
Share on other sites

So you are comparing the string's length to be greater or equal to one, not an empty string?

 

 

supposedly, comparing the string's index content to be greater or equal to one. 

 

Here's an example:

 

User's input is Qwert1 Yu

 

without the name >= '1', the condition would terminate. but since I added name >= '1', it wouldn't

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

supposedly, comparing the string's index content to be greater or equal to one. 

 

Here's an example:

 

User's input is Qwert1 Yu

 

without the name >= '1', the condition would terminate. but since I added name >= '1', it wouldn't

Ya but what are you checking to be >= '1' and why? I need more information to help. Is it the length of the string being longer or equal to one character or the unicode values being greater than 1 in unicode which is just 31?

Link to comment
Share on other sites

Link to post
Share on other sites

Ya but what are you checking to be >= '1' and why? I need more information to help. Is it the length of the string being longer or equal to one character or the unicode values being greater than 1 in unicode which is just 31?

it's the content of the string array. if name[x] would result to a number, the if condition will terminate.

Qwer1 Yname[0] = Q // Not terminatename[1] = w // not terminatename[2] = e // not terminatename[3] = r // not terminatename[4] = 1 // terminate herename[5] = space // skipsname[6] = Y // skips

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

You're not checking that a character in the string is >= '1', you are checking that the entire string is >= '1'.

Do you mean to do name[x] >= '1'?

Link to comment
Share on other sites

Link to post
Share on other sites

You're missing the .Length extension when you compare your string to 1. A string can only be compared to other strings, so depending on how c# does things (can't remember for sure) with that syntax it will either check if the string is '1' or it will simply give an error (seems to be the latter). string.Length on the other hand is an integer and can be safely compared with another integer (meaning you need to remove the " ' "s). The other comparisons seem correct, provided your intention is to compare the xth character to 'A', 'Z' etc.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

You're missing the .Length extension when you compare your string to 1. A string can only be compared to other strings, so depending on how c# does things (can't remember for sure) with that syntax it will either check if the string is '1' or it will simply give an error (seems to be the latter). string.Length on the other hand is an integer and can be safely compared with another integer (meaning you need to remove the " ' "s). The other comparisons seem correct, provided your intention is to compare the xth character to 'A', 'Z' etc.

I have that prior to the condition. the if condition is inside a for loop

for (int x = 0; x <= name.Length; x++)

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

I have that prior to the condition. the if condition is inside a for loop

for (int x = 0; x <= name.Length; x++)

 

Yeah, I had imagined that and that part seems correct. If you use .Length and 1 instead of '1' you should be set.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, I had imagined that and that part seems correct. If you use .Length and 1 instead of '1' you should be set.

Thanks. Worked perfectly :D

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×