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 returnStringIdsSeperatedWi thCommas() {
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
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 returnStringIdsSeperatedWi
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
Sorry that should have been
sb.append(SysCode[SysCode.length - 1]);
ASKER
ok THX. but why do we need >>sb.append(SysCode[SysCod e.length - 1]);
>>ok THX. but why do we need >>sb.append(SysCode[SysCod e.length - 1]);
Puts in the last element after the last comma
Puts in the last element after the last comma
ASKER
ok,, cant we just do
for (int i = 0;i < SysCode.length;i++) {
sb.append(SysCode[i]).appe nd(',');
}
for (int i = 0;i < SysCode.length;i++) {
sb.append(SysCode[i]).appe
}
ASKER
ok ,i got it..
thanks
thanks
Anything more?
ASKER
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]).appe nd(',');
}
sb.append(SysCode[SysCode. length - 1]);
}
return sb.toString();
}
but if (Codes != null) is false, it throws null pointer exception. any ideas?
thanks
J
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]).appe
}
sb.append(SysCode[SysCode.
}
return sb.toString();
}
but if (Codes != null) is false, it throws null pointer exception. any ideas?
thanks
J
ASKER
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]).appe nd(',');
}
sb.append(SysCode[SysCode. length - 1]);
}
return sb.toString();
}
public String commaSeperatedStrings()
{
StringBuilder sb = null;
if (SysCode != null)
{
for (int i = 0;i < SysCode.length- 1;i++)
{
sb.append(SysCode[i]).appe
}
sb.append(SysCode[SysCode.
}
return sb.toString();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
:-)
Open in new window