How to combine two integers and one text string?

0

Currently I have the dilemma that I want to do a certain operation with different basic math signs (add, subtract, multiply etc.). But I want the sign to be the only one that changes when doing the operation:

Previous code

resultado = resultado + numero; // Este código me daba el resultado de la suma  de ambos números 

Current code:

resultado = resultado +operadores+ numero; // este código me marca error

The line that marks me error is to what is due, I'm putting a string of text inside a variable of type int with two variables that are also whole, what I wanted to do is replace the "+" that made me the sum for another "+ o - o * o /" through the assignment to a variable and that depending on the selected button this would send me the necessary operator. The truth is I do not know if I am developing it in the best way but it is the only one that occurred to me at the moment and I wanted to see if I was able to do what I am trying to do. if I convert it to a string, the result variable will print everything and will not give me any resulting number.

Complete code of the method

public void igualdad(View v) {

    TextView txtSI = (TextView) findViewById(R.id.txtView);
    obtenerNS = txtSI.getText().toString().split("\+");

    for (int i = 0; i < obtenerNS.length; i++) {
        int numero = Integer.parseInt(obtenerNS[i].trim());
        resultado = resultado +operadores+ numero;
    }

    txtSI.setText("");
    txtSI.setText(String.valueOf(resultado));

}
    
asked by David 12.10.2017 в 21:14
source

2 answers

3

You can also use the statement switch

public double calculo(operadores, numero){
    double resultado = 0;
    operadores = operadores.replace(" ", "");
    switch (operadores) {
         case "+":
             resultado = resultado + numero;
             break;
         case "-":
             resultado = resultado - numero; 
             break;
         case "*":
             resultado = resultado * numero;
             break;
         case "/":
             resultado = resultado / numero;
             break;
         default:
             throw new IllegalArgumentException("Operador incorrecto");
    }
    return resultado;
}

Always remember to clean the operadores String for any space that could be.

To optimize your code, you can create a function and call it within your cycle as you say.

resultado = calculo(operadores, numero);
    
answered by 12.10.2017 / 22:42
source
1

You need to do a treatment to the variable operator , example:

public float Resultado(float resultado, float numero, char operador)
{
if (operador == '+')
    resultado = resultado + numero;
else if (operadores == '-')
    resultado = resultado - numero;
else if (operadores == '*') 
    resultado = resultado * numero;
else if (operadores == '/')
  {
     if (numero!=0) 
       resultado = resultado / numero;
     else throw new IllegalArgumentException("División por 0");
  }
else throw new IllegalArgumentException("Operador incorrecto");

return resultado;
}

In this way you can solve simple binary operations if they are composed, use Polish notation.

link

    
answered by 13.10.2017 в 15:14