LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Mobile
User Name
Password
Linux - Mobile This forum is for the discussion of all topics relating to Mobile Linux. This includes Android, Tizen, Sailfish OS, Replicant, Ubuntu Touch, webOS, and other similar projects and products.

Notices


Reply
  Search this Thread
Old 12-07-2019, 09:43 PM   #1
samir.18
LQ Newbie
 
Registered: Jul 2019
Posts: 3

Rep: Reputation: Disabled
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
 
Old 12-07-2019, 10:57 PM   #2
samir.18
LQ Newbie
 
Registered: Jul 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
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);
        }
    }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning a value to a variable also creates an empty file named as the value in bash StorageDon Linux - Newbie 3 11-01-2016 08:52 PM
Scripting Help--Check empty string condition (not null, but empty!) sungchoiok Linux - Newbie 4 01-01-2012 03:46 PM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
input single integer value, output the value as separate digits hubabuba Programming 2 03-02-2005 10:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Mobile

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration