Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several string inputs for one variable

Is there any other way to shorten this condition?

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

I was just wondering if there's something I can do to 'shortcut' this. The user will type a string when prompted what kind of operation is to be performed in my simple calculator program. Our professor said our program should accept whether the user enters "add", or "Add", you know, in lowercase letters or not... Or is the only way I should do it?

like image 632
Ciara Avatar asked Jan 24 '13 12:01

Ciara


People also ask

How do you take multiple inputs from one variable?

Using split() method : This function helps in getting a multiple inputs from user. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, user use a split() method to split a Python string but one can use it in taking multiple input.

How do you take multiple inputs of string?

For example, if want to take input a string or multiple string, we use naxtLine() method. It is only a way to take multiple string input in Java using the nextLine() method of the Scanner class.

How do you store multiple inputs in one variable python?

Using Split () Function With the help of the split () function, developers can easily collect multiple inputs in Python from the user and assign all the inputs to the respective variables. Developers can specify a character that will be used as a separator to break the input provided by the user.

Can you have multiple inputs in a function?

A multivariable function is just a function whose input and/or output is made up of multiple numbers. In contrast, a function with single-number inputs and a single-number outputs is called a single-variable function.


3 Answers

You can use String#equalsIgnoreCase(String) for 1st four strings: -

if (oper.equalsIgnoreCase("add") || 
    oper.equalsIgnoreCase("addition") || 
    oper.equals("+"))

If number of strings increases, you would be better off with a List, and use its contains method. But just for these inputs, you can follow this approach only.


Another way to approach this is to use String#matches(String) method, which takes a regex: -

if (oper.matches("add|addition|[+]")

But, you don't really need a regex for this. Specially, this method can become ugly for greater inputs. But, it's just a way for this case. So, you can choose either of them. 1st one is more clear to watch on first go.


Alternatively, you can also use enum to store operators, and pass it's instance everywhere, rather than a string. It would be more easy to work with. The enum would look like this:

public enum Operator {
    ADD,
    SUB,
    MUL,
    DIV;
}

You can enhance it to your appropriate need. Note that, since you are getting user input, you would first need to identify the appropriate enum instance based on it, and from there-on you can work on that enum instance, rather than String.

like image 134
Rohit Jain Avatar answered Sep 17 '22 23:09

Rohit Jain


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

In case of comparison of strings, if oper is null a NullPointerException could be thrown. SO its always better to write

"addition".equalsIgnoreCase(oper)

instead of

oper.equalsIgnoreCase("addition")
like image 22
TechSpellBound Avatar answered Sep 20 '22 23:09

TechSpellBound


If aDD is considered as invalid input, you can consider 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))
{
    // ...
}
like image 20
Azodious Avatar answered Sep 21 '22 23:09

Azodious