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);
LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
if u implemented equal in HouseDocumentCharges as I told u before then instead of the for loop do this:
ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
if(hoursesArray.indexOf("045") == -1
{
 // then the Array dosn't have HouseDocumentCharges with chargeId equals "045"
                         houseDocumentCharges.chargeId="045";
                        houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                        houseDocumentCharges.paymentAmount=0.00;
                        chargesList.add(houseDocumentCharges);

}
// continue as before

.....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chaitu chaituAuthor Commented:
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;
0
petmagdyCommented:
then u must check for null first like this
                ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                if(hoursesArray != null)
                {
                      //then continue
                }
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

zzynxSoftware engineerCommented:
replace

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

by

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

0
zzynxSoftware engineerCommented:
mmm... that's what petmagdy just said.
You see: we agree ;°)
0
chaitu chaituAuthor Commented:

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);
                }
            }
         }
0
chaitu chaituAuthor Commented:
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);
                }
0
chaitu chaituAuthor Commented:
now in my hoursesArray 162,205 charge codes r there but only one 045 charge code should added but it is adding twice;why?
0
zzynxSoftware engineerCommented:
It's rather strange to write those lines (see my remarks):

         if(breakBulkHouseModel.getChargesList() == null || breakBulkHouseModel.getChargesList().size() == 0)
         {
              // Here breakBulkHouseModel's chargeList is empty or null    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            if(helper.nextOperation.equals("Refresh") && breakBulkHouseModel.originStationType.equals("N") && 
               breakBulkHouseModel.houseFreightTerms.equals("Collect"))
            {
                ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                // if(hoursesArray.indexOf("045") == -1)  // <<<<<<<<<<<<<<<<<<<<<<<<  You don't need this check,
                                                                                 // there can't be anyhting in it since it's empty or null

                if (hoursesArray==null) {  // <<<<<<< you need this check
                    hoursesArray = new ArrayList();   // if null, create one
                    breakBulkHouseModel.setChargesList( hoursesArray );  // define it as belonging to breakBulkHouseModel
                }
               
                houseDocumentCharges.chargeId="045";
                houseDocumentCharges.chargeDescription="COLLECT INTERNATIONAL AIR FREIGHT";
                chargesList.add(houseDocumentCharges);
            }
         }
0
zzynxSoftware engineerCommented:
Wooops, you have to know: when I posted my previous comment, I didn't read you last three comments.
0
petmagdyCommented:
I can't understand ur last comment chaituu
0
zzynxSoftware engineerCommented:
>> I didn't read you last three comments
Although it is no answer to you Q about the doubles, it is still valid ;°)
0
zzynxSoftware engineerCommented:
Answer:
replace
>> if(hoursesArray.indexOf("045") != -1)
by
     if(hoursesArray.indexOf("045") == -1)

0
chaitu chaituAuthor Commented:
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);
                }
            }
         }
0
zzynxSoftware engineerCommented:
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 :)

0
zzynxSoftware engineerCommented:
Don't forget my previous remark...
0
chaitu chaituAuthor Commented:
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;



0
petmagdyCommented:
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
0
chaitu chaituAuthor Commented:
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;

0
zzynxSoftware engineerCommented:
>> 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"

0
zzynxSoftware engineerCommented:
Thanks for accepting
If your not completely happy with our answers (hence the B-grade I assume) don't hesitate to ask for better ;°)
0
chaitu chaituAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.