My class implements a very simple prototype of an RPN calculator.
The following construct isn't working. Why? What am I doing wrong here?
public boolean executeCommand(String command) {
if(command == "+") { add(); return true; }else
if(command == "-") { subtrair(); return true; }else
if(command == "*") { multiplicar(); return true; }else
if(command == "/") { dividir(); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
The output is always, no matter what the string contains,
The command does not exist.
Why? I really don't get it! If someone could please explain, I'd be thankful!
In more detail
The method in question is:
public boolean executeCommand(String command) {
Scanner str = new Scanner(command);
if (str.hasNextDouble()) {
dataStack.push(str.nextDouble());
return true;
} else {
System.out.format(" DEBUG: command: %s$%n", command);
if(command == "+") { add(); return true; }else
if(command == "-") { subtract(); return true; }else
if(command == "*") { multiply(); return true; }else
if(command == "/") { divide(); return true; }else
if(command == ".") { print(); return true; }else
if(command == ".s") { showStack(); return true; }else
if(command == "exit") { exit(); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
}
which, for any input I threw at it, (except for numbers, of course), resulted in:
DEBUG: command: [COMMAND HERE]$
The command does not exist.
Source code
I removed some irrelevant source code; (i.e. some methods, package name) but it is still compilable and runnable:
import java.util.Scanner;
import java.util.LinkedList;
public class RPNCalculator {
public static void main(String[] args) {
RPNCalculator calc = new RPNCalculator();
calc.startInteractiveMode();
}
protected Scanner scanInput;
public LinkedList<Double> dataStack;
protected boolean interactiveModeEnabled;
public RPNCalculator() {
scanInput = new Scanner(System.in).useDelimiter("\\s+");
dataStack = new LinkedList<Double>();
}
public String getCommand() {
return scanInput.next();
}
public boolean executeCommand(String command) {
Scanner str = new Scanner(command);
if (str.hasNextDouble()) {
dataStack.push(str.nextDouble());
return true;
} else {
System.out.format(" DEBUG: command: %s$%n", command);
if(command == "+") { ommitedOp("add"); return true; }else
if(command == "-") { ommitedOp("subtract"); return true; }else
if(command == "*") { ommitedOp("multiply"); return true; }else
if(command == "/") { ommitedOp("divide"); return true; }else
if(command == ".") { ommitedOp("print"); return true; }else
if(command == ".s") { ommitedOp("showStack");return true; }else
if(command == "exit"){ ommitedOp("exit"); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
}
public void startInteractiveMode() {
interactiveModeEnabled = true;
while (interactiveModeEnabled) {
String command = getCommand();
executeCommand(command);
}
}
public void ommitedOp(String method){
System.out.println("Command exists!");
}
}