How to compare two different values in java? Example [+ and -]

0

What I want to do is compare to my current operator + or any other arithmetic operator with any of the operators that I have previously that are also arithmetic,

I have 4+- , this here is wrong so I want to make it detect if there is a sign before already written do not do anything to me and send error.

remover=txtResultado.getText().toString();
String substringCadena=remover.substring(0,remover.substring.lenght-1);
        if (elemento.toString().equals(substringCadena)) {
            Toast toast = Toast.makeText(this, "No puede haber dos operadores consecutivos", Toast.LENGTH_SHORT);
            toast.show();
            contaOp=0;
        return;
        }

The problem with that is that if I have ++ the error is going to mark me, but if I have +- the error does not mark me and it is because they are not the same. so how do I make this possible?

    
asked by David 25.04.2018 в 02:46
source

2 answers

0

I will try to explain to you, since it seems that there is something that is not clear. Your code has a lot of problems first:

//Aquí parece que asignas a remover el contenido en texto de un objeto llamado "txtResultado"

remover=txtResultado.getText().toString();

//El problema es que a continuación dices que "substringCadena" es resultado de hacer el substring desde Cero a la longitud de 'remover.substring'. Esto no tiene sentido, ya que "remover.substring" como tal, no existe. Por otra parte, llamas a la longitud total -1 por lo que si fuera remover.substring(0,remover.lenght-1) únicamente te cogería todos los valores y nada más (no haría ningún substring).

String substringCadena=remover.substring(0,remover.substring.lenght-1);

//Aquí comparas la variable "elemento" que tampoco existía antes, por lo que nosotros en stackoverflow sabemos ni qué tiene, por lo que resulta imposible saber lo que quieres.
        if (elemento.toString().equals(substringCadena)) {
            Toast toast = Toast.makeText(this, "No puede haber dos operadores consecutivos", Toast.LENGTH_SHORT);
            toast.show();
            contaOp=0;
        return;
        }

If you tell us a little more and add the missing statements, or explain what each variable is, maybe we can help you more.

    
answered by 25.04.2018 в 12:17
0

The option that I would use would be to go through the entire chain with a for and enable a flag when it finds an operator so if it finds another operator while the flag is active you send the error something like this:

String txt=txt.getText().toString();

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

int i,j;

boolean ban=false;

for(i=0;i<txt.length();i++)
{
    for(j=0; j<op.length;j++)
    {
          if((txt.chatAt(i)+"").equals(op[j]))//Compara el caracter actual de la cadena con un operador y si es igual entra
          {
                if(ban)//Si la bandera es verdadera es porque ya se habia encontrado un operador antes por lo tanto se despliega el error
                {
                     System.out.println("error");
                     ban=false;
                }
                else
                     ban=true;//Si no se había encontrado un operador antes la bandera se pone en estado verdadero
                break;
          }
    }
    if(j==op.lenght)//En caso de que no encuentre un operador significa que es un numero por lo que la bandera se pone en falso
         ban=false;
 }
    
answered by 25.04.2018 в 18:10