Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

convert array items to string seperated with commas

hi guys

this is my code in my backend class

CodeVO[] oldCodes = ((ConVO)Object).getSysCode();
so oldCodes  is an array of ids
  =  [a,b,c,d,e]  (for example)

 
In ConVO , i have

public class ConVO
{
      public CodeVO[] SysCode            = null;

      public CodeVO[] getSysCode() {
            return SysCode;
      }
      
      public void setSysCode(CodeVO[] SysCode) {
            this.SysCode = SysCode;
      }

}
Now in ConVO, i want to write a method where i have to return a string with all the ids in array 'SysCode'
seperated by comma

something like


public String returnStringIdsSeperatedWithCommas() {
            if (SysCode != null)
            {
                  convert the SysCode array to a string of ids seperated with comma
               
             return the string;
                                       //so return something like   a,b,c,d,e
            }
             
      }
 
any ideas greatly appreciated

thanks
J
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can do something like
StringBuilder sb = new StringBuilder();
for (int i = 0;i < SysCode.length - 1;i++) {
	sb.append(SysCode[i]).append(',');
}
sb.append(SysCode[length - 1]);
return sb.toString();

Open in new window

Sorry that should have been
sb.append(SysCode[SysCode.length - 1]);

Open in new window

Avatar of jaggernat
jaggernat

ASKER

ok THX. but why do we need >>sb.append(SysCode[SysCode.length - 1]);
>>ok THX. but why do we need >>sb.append(SysCode[SysCode.length - 1]);

Puts in the last element after the last comma
ok,, cant we just do

for (int i = 0;i < SysCode.length;i++) {
      sb.append(SysCode[i]).append(',');
}
ok ,i got it..

thanks
Anything more?
yeah, last thing i guess
this is the code
      public String commaSeperatedStrings()
      {
            StringBuilder sb = null;
            if (Codes != null)
            {
                  for (int i = 0;i < SysCode.length- 1;i++)
                  {
                        sb.append(SysCode[i]).append(',');
                  }
                  sb.append(SysCode[SysCode.length - 1]);                   
            }
            return sb.toString();
      }


but  if (Codes != null)  is false, it throws null pointer exception. any ideas?

thanks
J
sorry , slight change

  public String commaSeperatedStrings()
      {
            StringBuilder sb = null;
            if (SysCode != null)
            {
                  for (int i = 0;i < SysCode.length- 1;i++)
                  {
                        sb.append(SysCode[i]).append(',');
                  }
                  sb.append(SysCode[SysCode.length - 1]);                  
            }
            return sb.toString();
      }

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
thanks
:-)