Article 4W8GK Check EMPTY value(input)

Check EMPTY value(input)

by
samir.18
from LinuxQuestions.org on (#4W8GK)
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 muchlatest?d=yIl2AUoC8zA latest?i=E7lKaQwaW8c:qHBxBvnLW9o:F7zBnMy latest?i=E7lKaQwaW8c:qHBxBvnLW9o:V_sGLiP latest?d=qj6IDK7rITs latest?i=E7lKaQwaW8c:qHBxBvnLW9o:gIN9vFwE7lKaQwaW8c
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments