Your problem is here:
String add = input.next();
String multiply = input.next();
String divide = input.next();
String subtract = input.next();
Each of these lines will ask the Scanner
for input and assign it to separate strings. You can show what is actually happening here by using a debugger or adding a System.out.println(add);
statement after the block above. Then you do this:
if (input.equals(add)) {
You're now checking to see if the Scanner
object is equal to the add
object. Since these are different types they will never be equal.
What you should do is first set up your options as constants:
static final String ADD = "add";
Then you will need to get the input:
String choice = input.nextLine();
Then you can check to see if the choice is equal to one of your constants.
if (choice.equals(ADD))
Note that next()
will return the next 'token', determined by the delimiter set up in the Scanner
. What you probably want is nextLine()
, which will get a whole line of input. In this case it probably doesn't matter, but if you're looking for a whole input it might. That said, there are some pitfalls you should learn about.
How to use Enumerations
Since it was asked in the comments, here is how you would implement this using an enumeration. As you can see, this becomes very extensible as you need to add operators: you no longer need to worry about modifying your main
function, and can simply add new operators and their behavior. You would do this only if Commands
never need to carry state, but only behavior, as they do here. This sort of setup is particularly useful with command-line programs that have to understand a variety of tokens (you can implement equals
or other methods to allow +
and Add
and add
to all be equivalent, for instance).
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 2 numbers ");
int numb1 = input.nextInt();
int numb2 = input.nextInt();
System.out.println("What would you like to do to these two numbers? Commands available are:");
for (Command command : Command.values()) {
System.out.println(command);
}
System.out.println("");
String operator = input.next();
for (Command command : Command.values()) {
if (command.toString().equals(operator)) {
command.printResult(numb1, numb2);
}
}
}
public interface CommandInterface {
public void printResult(int x, int y);
}
public enum Command implements CommandInterface {
ADD {
public void printResult(int x, int y) {
System.out.println(x + " + " + y + " is: " + (x + y));
}
},
SUBTRACT {
public void printResult(int x, int y) {
System.out.println(x + " + " + y + " is: " + (x + y));
}
},
MULTIPLY {
public void printResult(int x, int y) {
System.out.println(x + " + " + y + " is: " + (x + y));
}
},
DIVIDE {
public void printResult(int x, int y) {
System.out.println(x + " + " + y + " is: " + (x + y));
}
};
}
}
A run-time example:
Enter 2 numbers
1 2
What would you like to do to these two numbers? Commands available are:
ADD
SUBTRACT
MULTIPLY
DIVIDE
ADD
1 + 2 is: 3
Process finished with exit code 0