Multiple string entries for one variable

Is there any other way to reduce this condition?

if (oper.equals("add") || oper.equals("Add") || oper.equals("addition") || oper.equals("Addition") || oper.equals("+")) 

I'm just wondering if there is anything that I can do to “cut” it. The user will enter a string when prompted which operation should be performed in my simple calculator program. Our professor said that our program should accept whether the user enters "add" or "Add", you know, in lower case or not ... Or is this the only way to do this?

+6
source share
5 answers

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.

+9
source

In addition to @Rohit's answer, I would like to add this.

In case of string comparison, if oper is null a NullPointerException can be thrown. SO is always better to write

 "addition".equalsIgnoreCase(oper) 

instead

 oper.equalsIgnoreCase("addition") 
+4
source

If aDD is considered invalid, you can consider the following approach:

 ArrayList<String> possibleInputs = new ArrayList<String>(); possibleInputs.add("Add"); possibleInputs.add("add"); possibleInputs.add("Addition"); possibleInputs.add("addition"); possibleInputs.add("+"); if(possibleInputs.contains(oper)) { // ... } 
+3
source

throw away the entire code fragment inside the called function: isOperationAddition(String s){...} , which returns a boolean value.

So this is:

 if (oper.equals("add") || oper.equals("Add") || oper.equals("addition") || oper.equals("Addition") || oper.equals("+")){...} 

Changes in this

 if (isOperationAddition(operation)){...} 

Then, inside this method, do not use strings as branch material for your if statements. Have a variable that determines what type of operation it is and "Keep barbarians (confusion / ambiguous users) on the moat." You do not have to always go through the list to remember what operation we are dealing with.

0
source

You can take the input and convert it to lowercase, and then compare.

 str.toLowerCase() 

then go to your if () statement

 if(str.equals("add") || str.equals("addition") || str.equals("+")) 
0
source

All Articles