0

Here is my code

LDAPAttributeSet attributeSet = new LDAPAttributeSet();

String rolesName;
String uniquemem[] = rolesName.split(",");

if (uniquemem.length == 1)
    attributeSet.add(new LDAPAttribute("uniqueMember",
                        new String[] { "cn="+uniquemem[0]}));

if (uniquemem.length == 2)
    attributeSet.add(new LDAPAttribute("uniqueMember",
                        new String[] {
                                "cn=" +uniquemem[0],   
                                "cn=" + uniquemem[1]
                                    }));
if (uniquemem.length == 3)
    attributeSet.add(new LDAPAttribute("uniqueMember",
                        new String[] {
                                "cn=" + uniquemem[0]
                                        ,
                                "cn=" + uniquemem[1]
                                         ,
                                "cn=" + uniquemem[2]
                                          }));

I have tried by using List<String> and StringBuffer in forloop to add/append content dynamically but it is accepting only String[]. Please help me solve this issue

I have tried this

StringBuffer sb=new StringBuffer()
            for(int i=0;i<uniquemem.length;i++)
            {
                sb.append("cn=" + uniquemem[i]);
            }

attributeSet.add(new LDAPAttribute("uniqueMember",sb.toString()));
6
  • 2
    Can you clarify your question? What doesn't work here? Commented Jan 4, 2014 at 12:58
  • attributeSet is not defined... Commented Jan 4, 2014 at 12:59
  • @Mureinik I want to add element in String[] dynamically based on the length of uniquemem[] Commented Jan 4, 2014 at 13:00
  • What is the issue with your code ? BTW it miss a "," in the condition if (uniquemem.length == 2)after "cn=" +uniquemem[0] Commented Jan 4, 2014 at 13:07
  • Any error messages ? Your code looks good to me Commented Jan 4, 2014 at 13:08

3 Answers 3

2

This will give you the String array you need:

ArrayList<String> memList = new ArrayList<>(uniquemem.length);
for(int i = 0; i < uniquemem.length; i++) {
    memList.add("cn=" + uniquemem[i]);
}
memList.trimToSize();
memList.toArray(String[0]);
String[] memArray = memList.toArray(new String[memList.size()]);
Sign up to request clarification or add additional context in comments.

Comments

2

You can change the existing uniqemem array like this

for(int i=0;i<uniquemem.length;i++)
{
    uniquemem[i] = "cn=" + uniquemem[i];
}
attributeSet.add(new LDAPAttribute("uniqueMember", uniquemem));

Comments

1

What about this..

LDAPAttributeSet attributeSet = new LDAPAttributeSet();

String rolesName;
String uniquemem[] = rolesName.split(",");

String item = null;
String arrayItems[uniquemem.length]

for(int i=0; i<uniquemem.length; i++) {
    item = "cn=" + uniquemem[i];
    arrayItems[i] = item;
}


attributeSet.add(new LDAPAttribute("uniqueMember", arrayItems);

Comments

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.