I’m working with Android app, I have 5 edit text, and 1 button to sum the integer number from those edit text. Sometime user can let several edit text blanks. The problem is when the user let it blank, the exception will arise and I don’t have any idea to handle it. These are my code
try
{
if (qty1 != null)
{
int jml1 = Integer.parseInt(qty1.getText().toString());
item1 = hrg1 * jml1;
}
else
{
qty1.setText("0");
}
if (qty2 != null)
{
int jml2 = Integer.parseInt(qty2.getText().toString());
item2 = hrg2 * jml2;
}
else
{
qty2.setText("0");
}
if (qty3 != null)
{
int jml3 = Integer.parseInt(qty3.getText().toString());
item3 = hrg3 * jml3;
}
else
{
qty3.setText("0");
}
if (qty4 != null)
{
int jml4 = Integer.parseInt(qty4.getText().toString());
item4 = hrg4 * jml4;
}
else
{
qty4.setText("0");
}
if (qty5 != null)
{
int jml5 = Integer.parseInt(qty5.getText().toString());
item5 = hrg5 * jml5;
}
else
{
qty5.setText("0");
}
int hasil = item1 + item2 + item3 + item4 + item5;
Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
total.setText(String.valueOf(hasil));
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Quantity can't be empty. Please type item quantity", Toast.LENGTH_LONG).show();
}
I have try to put condition with if but it doesn’t help much. Thanks.
Try this,
instead of,
This condition if
(qty1 != null)checks if your editText is null but it doesn’t check the data contained in it.Repeat the same for all the other editText values.