LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Mobile (https://www.linuxquestions.org/questions/linux-mobile-81/)
-   -   Check EMPTY value(input) (https://www.linuxquestions.org/questions/linux-mobile-81/check-empty-value-input-4175665589/)

samir.18 12-07-2019 09:43 PM

Check EMPTY value(input)
 
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

samir.18 12-07-2019 10:57 PM

Problem solved, best regards.

Code:


  public void estatus(View view) {
        String valor1 = in1.getText().toString();
        String valor2 = in2.getText().toString();


        if (valor1.length() == 0 || valor2.length() == 0){

            resultado.setText("Error");
        }else{
            int input_n1 = Integer.parseInt(valor1);
            int input_n2 = Integer.parseInt(valor2);
            int suma = input_n1 + input_n2;

            String valorsuma = String.valueOf(suma);
            resultado.setText(valorsuma);
        }
    }



All times are GMT -5. The time now is 06:48 AM.