i have 1 edittext with number input only.the problem is,suppose user types 1234 no in it and afterwrds if he wants to change it and for that when he wil press del key tat time wen he cmes at 2 and press one more time del key,application crashes.and i tried handling if edittext text length 0 case also but stil not wrkng
here is my code
input.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
final String in= input.getText().toString();//input is edittext
final int j=in.length();
Cursor ansof1=(Cursor) mSpinner.getSelectedItem();//1st spinner tks 1 value
String temp=ansof1.getString(1);
Cursor ansof2=(Cursor)mSpinner2.getSelectedItem();//for 2 spinner
String temp2=ansof2.getString(1);
Cursor cn = myDbHelper.selectcur(temp);
double ans1=cn.getDouble(3);
Cursor cm=myDbHelper.selectcur(temp2);
double ans2=cm.getDouble(3);
no = Integer.parseInt(in);
final double finalans=((ans1/ans2)*no);
NumberFormat formatter = new DecimalFormat("##,##,###");
if(temp.equalsIgnoreCase(temp2))
{
//dlgAlert.setMessage("OOpss..!! Both Currencies Are Same...!!");
text1.setText(no+" "+temp+" "+"="+" "+no+" "+temp2);
//dlgAlert.create().show();
}
else
text1.setText(no+" "+temp+" "+"="+" "+formatter.format(finalans)+" "+temp2);
}
@Override
public void afterTextChanged(Editable arg0)
{
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// TODO Auto-generated method stub
}
});
It’s because you try to convert empty string to number. So check for length of string like this,