iqa
iqa

Reputation: 39

check empty value in edit text

i get error when i want to check empty value in edittext that I create. It become error to my source code

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = etCode.getText().toString();
                String name = etName.getText().toString();
                float price = Float.parseFloat(etPrice.getText().toString());
                int qty = Integer.parseInt(etQty.getText().toString());
                if(code.length() != 0 && name.length() != 0 && price.length() != 0 && qty.length() != 0){
                 myDB.addData(code, name, price, qty);
                    etCode.setText("");
                    etName.setText("");
                    etPrice.setText("");
                    etQty.setText("");
                }else{
                    Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
                }
                }

        });

How to solve my problem ?

Upvotes: 0

Views: 147

Answers (7)

impossible
impossible

Reputation: 2500

You can try like this:

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String code = etCode.getText().toString();
            String name = etName.getText().toString();
            String priceStr = etPrice.getText().toString();
            String qtyStr = etQty.getText().toString();
            float price;
            int qty;
            if(code.length()==0) {
                Toast.makeText(MainActivity.this,"Your code is empty!",Toast.LENGTH_LONG).show();
            }
            if(name.length()==0) {
                Toast.makeText(MainActivity.this,"Your name is empty!",Toast.LENGTH_LONG).show();
            }
            if(priceStr.length()==0) {
                Toast.makeText(MainActivity.this,"Your price is empty!",Toast.LENGTH_LONG).show();
            }else {
                try {
                    price = Float.parseFloat(priceStr);
                }catch (Exception e) {
                    Toast.makeText(MainActivity.this,"Your price is not valid!",Toast.LENGTH_LONG).show();
                }
            }
            if(qtyStr.length()==0) {
                Toast.makeText(MainActivity.this,"Your qty is empty!",Toast.LENGTH_LONG).show();
            }else {
                try {
                    qty = Integer.parseInt(qtyStr);
                }catch (Exception e) {
                    Toast.makeText(MainActivity.this,"Your qty is not valid!",Toast.LENGTH_LONG).show();
                }
            }
                myDB.addData(code, name, price, qty);
                etCode.setText("");
                etName.setText("");
                etPrice.setText("");
                etQty.setText("");
            }

        });

Upvotes: 0

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

I believe your error java.lang.NumberFormatException: Invalid float: ""

that is because you are trying to convert empty value to float.

you can do as follow.

btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code = etCode.getText().toString();
            String name = etName.getText().toString();
            String price = etPrice.getText().toString();
            String qty = etQty.getText().toString();
            if(code.isEmpty() && name.isEmpty() && price.isEmpty() && qty.isEmpty(){
                float price1 = Float.parseFloat(etPrice.getText().toString());
                int qty1 = Integer.parseInt(etQty.getText().toString());
                myDB.addData(code, name, price1, qty1);
                etCode.setText("");
                etName.setText("");
                etPrice.setText("");
                etQty.setText("");
            }else{
                Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
            }
        }

    });

Upvotes: 1

Tung Tran
Tung Tran

Reputation: 2955

Try this:

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = etCode.getText().toString();
                String name = etName.getText().toString();
                float price = Float.parseFloat(etPrice.getText().toString());
                int qty = Integer.parseInt(etQty.getText().toString());
                if(code.length() != 0 && name.length() != 0 && price != 0 && qty != 0){
                 myDB.addData(code, name, price, qty);
                    etCode.setText("");
                    etName.setText("");
                    etPrice.setText("");
                    etQty.setText("");
                }else{
                    Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
                }
                }

        });

Upvotes: 0

Siddharth Patel
Siddharth Patel

Reputation: 205

Try this

    /*
     *  for checking String valid or not
     */
    public boolean validateString(String data) {
        return (data != null && !data.isEmpty() && data.length() > 0 && !data.equals("null"));
    }

if(validateString(edittext.getText().toString()){
//Valid String
}

Upvotes: 0

AbhayBohra
AbhayBohra

Reputation: 2117

You can check that way

if(etCode.getText().toString().trim().equalsIgnoreCase(""))
{
// etCode edittext is empty
}

Upvotes: 0

bko
bko

Reputation: 1208

try using

TextUtils.isEmpty(editText.getText().toString());

normally you can never have a null come out of a text field but you check if its empty with the above line. so both "" and null will give you back a true

Upvotes: 1

Rishabh Rawat
Rishabh Rawat

Reputation: 1204

Use the

if (TextUtils.isEmpty(edittext.getText().toString()) {
    Log.d("ERROR","Edittext is empty");
}

Upvotes: 0

Related Questions