1
I’m starting Android development (using JAVA language). I am developing an average calculation app that displays the average and the student’s situation (Approved, In Recovery and Failed). My question is how to reset or reset the app variables.
Note: Insert some conditions like:Do not receive values below 0 or above 10, and do not receive null values or spaces.
Example of execution:
1st Execution:
Note 1:9 Note 2:6 Average: 7
2nd Execution:
Note 1:8 Note 2:11 Average: 7 (Continues the same value as the previous execution)
How do I reverse this? Is there a specific command that can be inserted at the end of the app to reset the values?
Code of the app (Manifest):
package com.example.notasescolares;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Declarando objetos JAVA
EditText edtNota1,edtNota2;
TextView txtMedia,txtSituacao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Interligando o XML ao JAVA
edtNota1 = findViewById(R.id.edtNota1);
edtNota2 = findViewById(R.id.edtNota2);
txtMedia = findViewById(R.id.txtMedia);
txtSituacao = findViewById(R.id.txtSituacao);
}
//Quando clicado o botão acontece um evento
public void processar(View view) {
boolean ok = true;
//Lógica para não crashar o app
if(edtNota1.getText().toString().trim().isEmpty()){
ok = false;
edtNota1.setError(getText(R.string.msgErroNumVazio));
}
if(edtNota2.getText().toString().trim().isEmpty()){
ok = false;
edtNota2.setError(getText(R.string.msgErro));
}
if(ok == true) {
float nota1 = Float.parseFloat(edtNota1.getText().toString());
float nota2 = Float.parseFloat(edtNota2.getText().toString());
float media;
boolean numValido = true;
//Verificando numeros validos recebidos(0 a 10)
if(nota1 < 0 || nota1 > 10){
edtNota1.setError(getText(R.string.msgErro));
numValido = false;
}
if(nota2 < 0 || nota2 > 10){
edtNota2.setError(getText(R.string.msgErro));
numValido = false;
}
//Se valido execute o processamento
if(numValido) {
//Calculo da média
media = (nota1 + nota2) / 2;
//Definindo situação
txtMedia.setText(String.format("%.2f", media));
if(media >= 7) { //Aprovado
txtSituacao.setText(R.string.strAprovado);
}else if (media >= 5) { //Recuperação
txtSituacao.setText(R.string.strRecuperacao);
}else { //Reprovado
txtSituacao.setText(R.string.strReprovado);
}
}
}
}
}
I added those lines of code you suggested. But it says that the variable does not accept null(float and null incompatible types). And instead of null I tried to insert zero in the variables.But still it was the same thing.It is displaying the results of the 1st execution (correct data) and the 2nd execution (wrong data) not calculating but displaying the values of the 1st execution.
– Ronan
updated the answer, maybe putting the variables declaration when entering this method does not give this problem
– Matheus Ribeiro