You can use String#equalsIgnoreCase(String) for the first four lines: -
if (oper.equalsIgnoreCase("add") || oper.equalsIgnoreCase("addition") || oper.equals("+"))
If the number of rows increases, you will be better off with List and use its contains method. But only for these inputs you can only follow this approach.
Another way to approach this is to use the String#matches(String) method, which takes a regular expression: -
if (oper.matches("add|addition|[+]")
But for this you really do not need a regular expression. Specifically, this method can become ugly for large input data. But this is just a way for this case. Thus, you can choose any of them. The 1st of these is clearer to look at for the first time.
Alternatively, you can also use enum to store operators and pass its instance everywhere, not string . It would be easier to work with him. The listing will look like this:
public enum Operator { ADD, SUB, MUL, DIV; }
You can improve it according to your needs. Note that since you are getting user input, you first need to determine the corresponding enum instance based on it, and from there you can work on this enumeration instance, not String.
source share