[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding particular value in arraylist

Posted on 2004-12-01
22
Medium Priority
?
208 Views
Last Modified: 2010-03-31
   
      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);
0
Comment
Question by:chaitu chaitu
  • 10
  • 8
  • 4
22 Comments
 
LVL 13

Accepted Solution

by:
petmagdy earned 300 total points
ID: 12714853
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12714909
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12714921
then u must check for null first like this
                ArrayList hoursesArray = breakBulkHouseModel.getChargesList();
                if(hoursesArray != null)
                {
                      //then continue
                }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 12714957
replace

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

by

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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12714962
mmm... that's what petmagdy just said.
You see: we agree ;°)
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12714990

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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12715013
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12715031
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 450 total points
ID: 12715033
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12715040
Wooops, you have to know: when I posted my previous comment, I didn't read you last three comments.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12715045
I can't understand ur last comment chaituu
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12715056
>> I didn't read you last three comments
Although it is no answer to you Q about the doubles, it is still valid ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12715064
Answer:
replace
>> if(hoursesArray.indexOf("045") != -1)
by
     if(hoursesArray.indexOf("045") == -1)

0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12715073
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12715074
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12715100
Don't forget my previous remark...
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12715195
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12715230
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12715613
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12715717
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12716605
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12717338
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question