Using the if statement with the Serial monitor

Hello everyone i am very new to Arduino so this code might be wrong. But i need help. I am trying to create a program where when i ask the serial monitor a question it gives me a answer but for some reason it doesn't work. I hope you can help me here is my code.
String myname = "";

void setup() {
Serial.begin(9600);

}

void loop() {

Serial.println("Enter your name.");

while (Serial.available()==0)
{ //Wait for user input

}
myname = Serial.readString();

if (myname="Max")
{
Serial.println("hello");

}
else{

Serial.println("wut");

}
while(Serial.available()==0)
{
}
}

Issue with = instead of == in your if testif (myname="Max") // oops

You don’t need the last while since the loop will loop so you’ll go back to the top where you are actively waiting for input

You should read this Serial Input Basics and Planning and Implementing a Program

And of course you should read how to use the forum...

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

I didn't quite understand what you were trying to say. My apologies i am new so it might be difficult for me to understand.

 String myname = ""; 
  
void setup() {  
    Serial.begin(9600);  
   
}  
  
  
void loop() {  
    
Serial.println("Enter your name.");  
   
  while (Serial.available()==0)  
  {                                       //Wait for user input  
    
  }  
  myname = Serial.readString(); 
   
                     
   






    
  
 if (myname="Max")
 {
  Serial.println("hello");
  
  }
  else{
    
    Serial.println("wut");
    
    }
}

Also i removed the last while loop it did not do any changes. I want the serial monitor only to respond back to me when I type max in it. But it responds even if i don't type max. If you don't understand run the code for your self.

 if (myname="Max")

= for assignment, == for comparison. The if is a comparison.

Then what should i use??

AnonymooseYT:
Then what should i use??

== like you did here:

while (Serial.available()==0)

Bro just make it easy for me and give the code edited and explain it to me. The way you guys are explain is really confusing

Also when it comes to Strings it is good practice to use the reserve operation.
For example in setup
myname.reserve(10);

And give it enough space to accept the longest pissible input from the serial port by changung the number (10)

AnonymooseYT:
I didn't quite understand what you were trying to say. My apologies i am new so it might be difficult for me to understand.

 String myname = ""; 

void setup() {  
   Serial.begin(9600);  
 
}  
 
 
void loop() {  
   
Serial.println("Enter your name.");  
 
 while (Serial.available()==0)  
 {                                       //Wait for user input  
   
 }  
 myname = Serial.readString();

if (myname="Max")
{
 Serial.println("hello");
 
 }
 else{
   
   Serial.println("wut");
   
   }
}

Replace this

if (myname="Max")

With this

if (myname=="Max")

P.S.

Being a little more polite wont hurt. Remember that noone is obliged to help. We all do it to help each other from hart.

This only enables the else statement. Bro i want that when i type max in the serial monitor i get the reply "Hello" and if i type anything besides Max i get the reply "Wut". But when i type Max in the serial monitor i get the reply "Wut".

This works for me: ie, I got hello when I pretended to be Max. It's your code from #2 with the = replaced with == as explained.

(Format tidied up a bit)

String myname = "";

void setup()
{
  Serial.begin(9600);
}

void loop() {

  Serial.println("Enter your name.");

  while (Serial.available() == 0)
  { //Wait for user input

  }
  myname = Serial.readString();

  if (myname == "Max")
  {
    Serial.println("hello");
  }
  else
  {
    Serial.println("wut");
  }
}

What is the line-ending in serial monitor set to?

It's somewhere in the right bottom. I guess it's set to carriage return and line feed

"Max" is not the same as "Max\r\n".

Change the line ending to none.

sterretje:
What is the line-ending in serial monitor set to?

Good call: my test which worked was with it set to none. When I set it to either or both cr or lf, I get wut as expected.

Nothing like a "tight bottom"...

Doesn't work for me i keep getting "wut"

Can you please show your example in code

AnonymooseYT:
Can you please show your example in code

Take your code from #2 and make the == change from #9.

Check the Line Ending in the bottom right of Serial Monitor is set to "No line ending"

Line ending....

line end and baud.GIF

line end and baud.GIF

Thank you very much everyone i got it to work!!!!!