I am starting to learn java and reviewing exams to answer questions from previous exam documents, and there is one question I'm stuck with.
Consider the code snippet below that reads an input command, then processes it.
String cmd = scanner.next();
if (cmd == "forward" )
robot.forward(1);
else if (cmd == "turn" )
robot.turn();
else
System.out.println("Unknown command: " + cmd);
When testing the program, the scanner reads the line "forward" in cmd, but the program displays "Unknown command: forward".
a) Explain why this is happening.
b) What changes need to be made to the code to correct this error.
If someone can help me answer the question a) and b) I would be grateful.
p.s. , -, (#noeasywayout), . .