Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

adding particular value in arraylist

   
      when i click  refresh  when the charge list is null      i will add 045 charge code in houseDocumentCharges object and added into arraylist;

      in the else block if the charge list is there and chargecodes r there other than 045 chargecode, then i add 045 charge code in houseDocumentCharges object ,if  exists in chargelist i won't add 045 chargecode  in houseDocumentCharges object
     

finally i will set the list breakBulkHouseModel.setChargesList(chargesList);

      Is this logic fine;


     
      if(breakBulkHouseModel.getChargesList() == null || breakBulkHouseModel.getChargesList().size() == 0)
         {
            if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
           
                houseDocumentCharges.chargeId="045";
                houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                chargesList.add(houseDocumentCharges);
            }
         }
         else
         {
         if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                for(int i=0;i<breakBulkHouseModel.getChargesList().size();i++)
                  {
                       houseDocumentCharges =(HouseDocumentCharges)  breakBulkHouseModel.getChargesList().get(i);
                    if(houseDocumentCharges.chargeId.equals("045"))
                    {
                        break;
                   
                    }else
                    {
                         houseDocumentCharges.chargeId="045";
                        houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                        houseDocumentCharges.paymentAmount=0.00;
                        chargesList.add(houseDocumentCharges);
                         break;
                    }

                  }
            }
         }

//end
            breakBulkHouseModel.setChargesList(chargesList);
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chaitu chaitu

ASKER

if(breakBulkHouseModel.getChargesList() == null || breakBulkHouseModel.getChargesList().size() == 0)
         {
            if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                if(hoursesArray.indexOf("045") == -1)
                {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
                }
            }
         }

IF I DO LIKE THIS I AM GETTING NULLPOINTER EXCEPTION  AT THIS LINE hoursesArray.indexOf("045"); BACAUSE HhouseAray comes as null;
then u must check for null first like this
                ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                if(hoursesArray != null)
                {
                      //then continue
                }
Avatar of zzynx
replace

>> if(hoursesArray.indexOf("045") == -1)

by

     if(hoursesArray!=null && hoursesArray.indexOf("045") == -1)

mmm... that's what petmagdy just said.
You see: we agree ;°)

if block is ok but in the else block   chargelist is not null ,and chargecode 045 is not  then add ;other wise dont add 045 charge code;

but 045 value coming twice ;


  if(breakBulkHouseModel.getChargesList() == null || breakBulkHouseModel.getChargesList().size() == 0)
         {
            if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
               
            }
         }
         else
         {
         if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                  ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                if(hoursesArray.indexOf("045") != -1)
                {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
                }
            }
         }
i changed  in else block 045 charge codes coming twice

   if(hoursesArray.indexOf("045") == -1)
                {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
                }
now in my hoursesArray 162,205 charge codes r there but only one 045 charge code should added but it is adding twice;why?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wooops, you have to know: when I posted my previous comment, I didn't read you last three comments.
I can't understand ur last comment chaituu
>> I didn't read you last three comments
Although it is no answer to you Q about the doubles, it is still valid ;°)
Answer:
replace
>> if(hoursesArray.indexOf("045") != -1)
by
     if(hoursesArray.indexOf("045") == -1)

if breakBulkHouseModel.getChargesList() is null ia m just adding 045 chargecode in the arraylist its ok;

but if breakBulkHouseModel.getChargesList() is not null say in the  breakBulkHouseModel.getChargesList()  (in this arraylist i have HouseDocumentChargesobject;) i have charge code 162,205 other than 045 chargecode  then i want to add 045 chargecode;


if(breakBulkHouseModel.getChargesList() == null || breakBulkHouseModel.getChargesList().size() == 0)
         {
            if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
               
            }
         }
         else
         {
         if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                  ArrayList hoursesArray = breakBulkHouseModel.getChargesList();

                if(hoursesArray.indexOf("045") == -1)
                {
                    houseDocumentCharges.chargeId="045";
                    houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                    chargesList.add(houseDocumentCharges);
                }
            }
         }
Explanation:

>> if(hoursesArray.indexOf("045") == -1)  means "045" is NOT in
you had:
>> if(hoursesArray.indexOf("045") != -1)  meaning "045" is already in (so as a result you had it twice :)

Don't forget my previous remark...
how the compiler knows that Housedocumentcharges .chargeId  045 is there

because hoursesArray is the Arraylis of Housedocumentcharges Object

 if(hoursesArray.indexOf("045") == -1) how this works;



the is because the indexOf call equals("045") for each element in ur ArrayList and if true returns the index of this elment if no matches return -1
Acctually the arrayList consists of  Housedocumentcharges objects which has member vairables like chargeId, paymentAmt, desc etc.


suppose if in the desc member variable of HouseDocumentCharges object contains text like "045"
then what will the indexOf function returns (1 or -1)?

acctually i want scenario like if chargeId member variable of HouseDocumentCharges object only contains 045 then it should return  true else false;

>> then what will the indexOf function returns (1 or -1)?
Depending on how you implement your indexOf() function

>> i want scenario like if chargeId member variable of HouseDocumentCharges object only contains 045 then it should return  true else false
Then you should check all Housedocumentcharges objects contained in the arrayList one by one if their chargeId equals "045"

Thanks for accepting
If your not completely happy with our answers (hence the B-grade I assume) don't hesitate to ask for better ;°)
actually ur answers solve my purpose i have to do like this to solve my problem

 for(int i=0;i<chargesList.size();i++)
                    {
                        houseDocumentCharges    =(HouseDocumentCharges)chargesList.get(i);
                         if(houseDocumentCharges.chargeId.equals("045"))
                          {
                            cnt++;
                          }
                   }
                        if(cnt == 0)
                        {
                            HouseDocumentCharges houseDocumentCharges1 = new HouseDocumentCharges();
                            houseDocumentCharges1.chargeId="045";
                            houseDocumentCharges1.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                            houseDocumentCharges1.paymentAmount=0.00;
                            chargesList.add(0,houseDocumentCharges1);
                           
                        }
and also
 if(hoursesArray.indexOf("045") == -1) not working correctly in my senario;

anyway thanks for ur help