Hi, I make first small project in AndroidStudio.
Simple SUM project.
RUN ok, no problems, except if:
-I leave one or both sum fields empty, the application closes when executing the method (press button).
I Am amateur not professional programer.
How to make a conditional check if both entries "have something" and are not blank?
The app only closes when executing the method and having one of the two entries without data, when placing data it never closes and does the sum.
Method area:
Code:
public void estatus(View view) {
String valor1 = in1.getText().toString();
String valor2 = in2.getText().toString();
int input_n1 = Integer.parseInt(valor1);
int input_n2 = Integer.parseInt(valor2);
if (input_n1 > 0 && input_n2 > 0){
int suma = input_n1 + input_n2;
String valorsuma = String.valueOf(suma);
resultado.setText(valorsuma);
}else{
resultado.setText("No campo");
}
Complete:
Code:
package com.example.suma2;
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 {
private EditText in1;
private EditText in2;
private TextView resultado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
in1 = (EditText)findViewById(R.id.input_n1);
in2 = (EditText)findViewById(R.id.input_n2);
resultado = (TextView)findViewById(R.id.casillaresultado);
}
public void estatus(View view) {
String valor1 = in1.getText().toString();
String valor2 = in2.getText().toString();
int input_n1 = Integer.parseInt(valor1);
int input_n2 = Integer.parseInt(valor2);
if (input_n1 > 0 && input_n2 > 0){
int suma = input_n1 + input_n2;
String valorsuma = String.valueOf(suma);
resultado.setText(valorsuma);
}else{
resultado.setText("No campo");
}
}
}
Thank you very much