0

I am passing some parameters in the URL and then I add them in a list. My list has a limit of 5 elements. So if someone adds 6th element in the URL the list would simply ignore it. So I am trying to use a counter but the logic is not working as desired. I am using While loop to achieve this. So if list size is smaller than 5 set the agencyCds otherwise just return the list.

private List<IUiIntegrationDto> generateViewIntegrationReportData(ESignatureIntegrationConfig eSignConfig) throws Exception {
    int counter = 1;
    if(eSignConfig.getAdditionalAgencyCds() != null ) {

        List<String> combinedAgencyCds = new ArrayList<String>();

        for(String agencyCd : eSignConfig.getAgencyCd()) {
            combinedAgencyCds.add(agencyCd);
        }

        StringTokenizer token = new StringTokenizer(eSignConfig.getAdditionalAgencyCds().toString(), StringConstants.COMMA);
        while(token.hasMoreTokens()) {
            combinedAgencyCds.add(token.nextToken());
        }

        while(combinedAgencyCds.size() < 5) {
            counter = counter + 1;
            eSignConfig.setAgencyCd(combinedAgencyCds);
        }

    //  eSignConfig.setAgencyCd(combinedAgencyCds);
    } 
    List<IUiIntegrationDto> intgList = getUiIntegrationManager().retrieveUiIntegrationReportData(eSignConfig.getAgencyCd(), eSignConfig.getCreatedDays(),
            eSignConfig.getLob(), eSignConfig.getTransactionStatus(), eSignConfig.getAccounts(), eSignConfig.getSortKey(), eSignConfig.getSortOrder());

    return intgList;
}

I am not completely sure about this logic if it is correct or if there is nay better approach.

Thanks

5
  • Are you sure that the combinedAgencyCds list is decreasing when you call eSignConfig.setAgencyCd()? If not, then you second while loop will be infinite. Also, what is counter used for? Commented Aug 24, 2016 at 14:19
  • yeah this is what i found out while debugging. I am using if condition now. Commented Aug 24, 2016 at 14:23
  • if(combinedAgencyCds.size() < 5) { counter = counter + 1; eSignConfig.setAgencyCd(combinedAgencyCds); } Commented Aug 24, 2016 at 14:23
  • So you want a combined list of agencyCds and additionalAgencyCds? What happens if there are already 5 or more agencyCds? Also your counter doesn't seem to do anything Commented Aug 24, 2016 at 14:23
  • So basically I don't want to add the 6th element. If elements are less than 5 add them in the list. And, yes they are combined. If there are already 5 elements fine but ignore the 6th one. Commented Aug 24, 2016 at 14:25

2 Answers 2

2

Try this instead of the last while in your code:

if(combinedAgencyCds.size() <= 5) {
   eSignConfig.setAgencyCd(combinedAgencyCds); 
} else {
   eSignConfig.setAgencyCd(combinedAgencyCds.subList(0, 5));
}

The full combined list will then be used if it is less than 5 in size. Otherwise, only the first 5 elements are used.

Edit: Or even better:

eSignConfig.setAgencyCd(combinedAgencyCds.subList(0, Math.min(5, combinedAgencyCds.size())));
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was looking for mdewit. This is awesome. It is working as desired. Thank you so much for your help. Such a great learning.
Cool no problem, glad it was useful :)
1

Ok so let's break down what your code is currently doing.

int counter = 1;

while(combinedAgencyCds.size() < 5) {
    counter = counter + 1;
    eSignConfig.setAgencyCd(combinedAgencyCds);
}

This snippet of code has a couple things wrong best I can tell. First, this loop has the possibility of running forever or not at all. Because combinedAgencyCds is never being manipulated, the size won't ever change and the logic being checked in the while loop never does anything. Second, there's a more efficient loop for doing this, assuming you don't need the counter variable outside of its usage in the while loop and that is using for loops.

Example syntax is as follows:

for (int i = 0; i < combinedAgencyCds.size(); i++) {
    if (i < 5) {        
        // Do your logic here.
    }
    else {
        break; // Or handle extra values however you want.
    }
}

Notice there is no need for the explicit declaration for a counter variable as "i" counts for you.

Now in your actual logic in the loop, I'm not sure what the setAgencyCd method does, but if it simply sets a list variable in the eSignConfig like it appears to, repeating it over and over isn't going to do anything. From what I can see in your code, you are setting a variable with the same value 5 times. If you need any more explanation just let me know and I will be happy to revise the answer.

1 Comment

That makes sense and I am not using the while loop because it was infinite. What I am trying to achieve is to count the number of elements in the list that is why I was using counter. If size of the list is 5 or less then 5 then set the agencyCds using set method else ignore the 6th element but add the rest you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.