Bivin
Bivin

Reputation: 373

number format exception invalid int

I have to add values to my sq lite from the list view. In my list view there are two edit texts and text view. I just want to get value from each edit text and multiply it to the corresponding text view value. when I am running the app, I need not to enter data to every edit text. due to this I am ending with a "number format exception : invalid int". From other examples I can understand that multiplication on null value may cause the error. how can I skip the null value contained edit texts from the iteration? this is my code

protected void InsertDb() {
// TODO Auto-generated method stub
 DatabaseHelper databasecontroller = new DatabaseHelper(Orders.this);
 List<Map<String, String>> data = null;
 data = new ArrayList<Map<String, String>>();

   if(list != null){

        for(int i = 0; i< list.getChildCount();i++){

            View vie = list.getChildAt(i);

            EditText ed1= (EditText) vie.findViewById(R.id.cases);
            EditText ed2 = (EditText) vie.findViewById(R.id.pcs);
            String qty = ed1.getText().toString();

           TextView tv = (TextView)findViewById(R.id.srp);
           String imsrpv= tv.getText().toString();

            float srps = Float.valueOf(imsrpv);
            int qtys = Integer.valueOf(qty);
           float amts = srps * qtys; 
           String amount = Float.toString(amts);

           datanum.put("A",qty );
            datanum.put("B",ed2.getText().toString() );
           datanum.put("L", amount);

            Log.d("value of amnt",amount);

            databasecontroller.entercustdetails(datanum); 
        }

           }
   Log.v("compleated", data.toString());
}

Thanks in advance..

Upvotes: 2

Views: 1195

Answers (2)

Opiatefuchs
Opiatefuchs

Reputation: 9870

Your problem is not how You have parsed the values. For explanation:

parseFloat(), parseInt() will return a primitive type of int and float and valueOf() returns a new type of Integer and Float. Also, parseInt(), parseFloat() will recognize plus and minus signs, valueOf() doesn´t. That´s the difference between these two types/methods. Your problem seems to be, that the value is empty and You can simply get rid of this by:

//set a default float and int

float srps = 0.0;
int qtys = 0;

//now parse the values by checking if the strings are not null or empty
if(imsrpv!=null&&!imsrpv.isEmpty()){

srps = Float.valueOf(imsrpv);

} 

if(qty!=null&&!qty.isEmpty()){

qtys = Integer.valueOf(qty);
}

Like I said, if these values have a plus or minus sign and it is important for Your needs, You should use the parse method. Also, You don´t need a new Integer or Float type, so more correct is using parse() method.

Upvotes: 1

Ankur1994a
Ankur1994a

Reputation: 2122

Check either returning value from edittext is a proper number or it is not null.if a null value is there it may give exeception of numberformat.you have to handle this. if both are correct then you can also you

float srps=0.0;
int qtys=0;
 try
{
  srps = Float.valueOf(imsrpv);
  qtys = Integer.valueOf(qty);
}
catch (NumberFormatException e)
{
srps =//Default value you want;
qtys =//Default value you want;
}

Hope it works..

Upvotes: 2

Related Questions