+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

1/11/2019 3:47:20 PM

Milanga

18 Answers

New Answer

+10

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"))

+7

I remember the first time i was searching why my code didnt work and i realised after many hours that you need == and not = to check equality

+5

string input = Console.ReadLine(); if (input.Equals("hi")) { Console.WriteLine("Congratz"); } if (input == "hi") { Console.WriteLine("Congratz"); } You can use '==' and '.Equals', it's up to you :)

+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.

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 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!

0

0

its already fixed, no?

0

= 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.

0

What language are you programming?

0

What if we wanna do greater or equal to?

0

This is code I corrected: String MyName = Console.ReadLine(); Notice I spelled string with a capital S

-1

I tested your code and did not have any errors! I used "Manuel" then "Sam" each time it branched to the correct output!

-1

Oh how can someone help you unless you send code with errors?