0
I’m developing a calculator that, at the end of the code, performs a function called mostrarResultado()
, this function receives the campoTexto
from the screen, Ex:("2+3"), and I want whatever the operator (which is stored inside an operation variable) to be the character that separates one String from the other.
However, when executing the code, a Fatal Exception
, in line 3 of the code below, of the function implemented so far. I thank you.
public void mostrarResultado(){
String texto = campoTexto.getText().toString();
String[] valores = texto.split(operacao);
float numA = Float.parseFloat(valores[0]);
float numB = Float.parseFloat(valores[1]);
float result = 0;
switch(operacao) {
case "+":
result = numA + numB;
break;
case "-":
result = numA - numB;
break;
case "*":
result = numA * numB;
break;
case "/":
result = numA / numB;
break;
}
campoTexto.setText(String.valueOf(result));
}
Where does this variable come from
operação
?– Maniero
It is a global variable that is used in another function as well. This other function is the one that stores the operator value within the operation variable.
– Bruno Toledo
And what’s in it. You have to put all the information you need to understand the code. You must [Dit] the question to put everything that is relevant.
– Maniero
@Brunotoledo, please indicate if my answer helps anything, because really the way your question is getting complicated help. If my answer does not answer, please signal so I can delete it. About your code, I saw that you changed the content of Split. Before it had a string called "operation", now it has a variable called operation. If you don’t put more code and indicate how you get this operation, it gets very complicated to help you.
– cantoni