if statement not working

Just starting out on this adventure. I am able to use Serial.parseInt for input to an if statement but cannot get input from a string using Serial.readString. The serial grabs it but it will not input to the if statement. Any help or comments appreciated.

String testText;

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:

Serial.println ("Enter a word");
while (Serial.available()==0) {}
testText=Serial.readString();
Serial.println(testText);

if (testText=="Jack"){

Serial.println("Hooray");


}

}

Print the received String in HEX to see if any invisible characters are coming in with the String.

Using String objects is not recommended with most Arduino boards. Read why.

The serial input basics thread shows how to reliably read serial input into strings (null terminated character arrays).

What have you got the Line ending set to in the Serial monitor ?

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

void loop() {
  Serial.println ("Enter a word");
  while (Serial.available() > 0) {
  }
  testText = Serial.readStringUntil('\n');
  Serial.println(testText);

  if (testText == "Jack") {
    Serial.println("Hooray");
  }
  Serial.flush();
}

This works pretty well.

By the way, your code works pretty well. be sure you write "Jack" with capital J.

My thanks to all of you. My line ending on the SM was set to Newline. Used the suggestion from GrOnThOs, Serial.readStringUntil('\n'), that works fine, appreciate the tip. Also understand avoiding use of strings is a good practice.

Glad it's working. As it happens, the way you're using String here looks safe and sane - where the evil of String really comes into play is when you repeatedly append things to a String. Which is easier than doing the same with c strings (null terminated char arrays), and why many novices are drawn to String.

Edit: Er, maybe not as safe as I initially thought - a long series of characters without the terminator you passed readstringuntil() will eventually end up building a String that can't fit in memory, and barfing. I think readStringUntil internally does the repetitive appending too (but you can get away with it as long as the string stays short).

Also understand avoiding use of strings is a good practice.

Using strings (lowercase s) is fine. Using Strings (capital S) can cause problems

My line ending on the SM was set to Newline.

That was the problem with your original code. The Newline was being included in what you read so never matched with the string constant that you were testing it against, hence it failed.

Think Newline is the default setting, I didn't know any better. Thanks.