+ 7
(Newbie question) if-else statement ignoring strings?
Now, I aknowledge this question may be a bit stupid but I couldn't find what was wrong with my code by myself so I thought I could ask for help on the forums. well, for context, I make simple codes in the code playground beetween courses to make sure that I can apply what I learned, but when I was testing if-else statements I was greeted with an error message that basically said that a string that I stored at the start of the code didn't exist. I attached my code with this post, so you can check it out to see what's wrong https://code.sololearn.com/clfkJiT8g5Lg/?ref=app
16 Answers
+ 11
To check for equality, use '==' instead of '='.
And place string Manuel in double quotes like "Manuel".
+ 9
To check for string equality use Equals methods:
if(MyName.Equals("Manuel"))
+ 4
perfect!!
+ 2
I think of ‘=‘ as ‘assign’ and == ( or === depending on language) as equals. This may help you too.
+ 2
Guys. Of course my code works. BECAUSE IT'S FIXED. SOMEONE HELPED ME OUT.
+ 1
I wrote a commercial C sharp program in Visual Studios 10 that uses Forms for inputs. It is Tutorial for individuals studying Electronics! I have always loved science and engineering!
+ 1
= is used for assigning a value to a varible.
== is used in logic decisions comparing two varible to see if is true they are equal to each other.
+ 1
What language are you programming?
+ 1
This is code I corrected:
String MyName = Console.ReadLine();
Notice I spelled string with a capital S
0
And I do not get an error, everything works
0
I found the bug! For C#, you don’t need the MyName.Equals(“text”). You just need an equality sign like MyName==“text here”. You do need to do that in Java, but not in C#! Here is a link to the fixed code. Link:
https://code.sololearn.com/c48J0Tcmtk69/?ref=app
0
I tested your code and did not have any errors! I used "Manuel" then "Sam" each time it branched to the correct output!
0
Oh how can someone help you unless you send code with errors?
0
Yes.
0
its already fixed, no?
0
What if we wanna do greater or equal to?