1

I have an array

String[] Array = {"+", "-", "*", "/"}

and an EditText string

operation = (EditText) findViewById(R.id.operation);

So, I have a button where I want to add an if statement like:

    Button.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(operation.getText().toString().endsWith(Array.toString())){
                        prinln("Example"); }
                }
            });

I don't know how to make this right. How to write the if statements where the String ends with any value of array and make it work

Hexley21
  • 566
  • 7
  • 16

2 Answers2

1

You need to loop through your array, this will take each of the string in the array and check if your String ends with the String 's' from the array

for (String s: Array) {
        //Do your stuff here
        if(yourString.endsWith(s)){
            println(example);
        }
    }
SABANTO
  • 1,316
  • 9
  • 24
1

If you really don't want to use a loop, you can save array values as keys and your desired answers as values in a map. And then use the map directly.

HashMap<char, String>operandMap = new HashMap<char,String>(); 
operandMap.put('+',"Addition");
operandMap.put('-',"Subtraction");
operandMap.put('*',"Multiplication");
operandMap.put('/',"Division");
operandMap.put('%',"Modulus");
println(operandMap.get(operation.getText().toString().charAt(operation.getText().toString().length-1)));
Varun Sharma
  • 177
  • 1
  • 9